String_manipulation
Chapter 13: String Manipulation
Section titled “Chapter 13: String Manipulation”Overview
Section titled “Overview”String manipulation is a core skill for any bash scripter. This chapter covers substring extraction, pattern matching, search and replace, case conversion, and advanced string operations using bash built-ins and external tools. These skills are essential for processing log files, parsing configuration files, and manipulating text data in DevOps workflows.
Basic String Operations
Section titled “Basic String Operations”String Length
Section titled “String Length”#!/usr/bin/env bashtext="Hello, World!"
# Get string lengthecho "${#text}" # 13
# Alternative using exprlength=$(expr length "$text")echo "$length" # 13String Concatenation
Section titled “String Concatenation”#!/usr/bin/env bashstr1="Hello"str2="World"
# Concatenateresult="$str1 $str2"echo "$result" # Hello World
# Appendstr1+=" World"echo "$str1" # Hello WorldSubstring Operations
Section titled “Substring Operations”Substring Extraction
Section titled “Substring Extraction”#!/usr/bin/env bashtext="Hello, World!"
# From position 0, length 5echo "${text:0:5}" # Hello
# From position 7, length 5echo "${text:7:5}" # World
# From position to endecho "${text:7}" # World!
# Last N charactersecho "${text: -5}" # World
# Using expr substrecho $(expr substr "$text" 1 5) # HelloPattern Matching
Section titled “Pattern Matching”Remove Pattern
Section titled “Remove Pattern”#!/usr/bin/env bashfilename="report-2024-01-15.csv"
# Remove shortest match from beginningecho "${filename#*-}" # 2024-01-15.csv
# Remove longest match from beginningecho "${filename##*-}" # csv
# Remove shortest match from endecho "${filename%.*}" # report-2024-01-15
# Remove longest match from endecho "${filename%%.*}" # reportReplace Pattern
Section titled “Replace Pattern”#!/usr/bin/env bashtext="Hello World"
# Replace first occurrenceecho "${text/World/Universe}" # Hello Universe
# Replace all occurrencesecho "${text//l/L}" # HeLLo WorLd
# Replace if at beginningecho "${text/#Hello/Hi}" # Hi World
# Replace if at endecho "${text/%World/Everyone}" # Hello EveryoneCase Conversion
Section titled “Case Conversion”Convert Case (Bash 4+)
Section titled “Convert Case (Bash 4+)”#!/usr/bin/env bashtext="Hello World"
# First character uppercaseecho "${text^}" # Hello World
# All characters uppercaseecho "${text^^}" # HELLO WORLD
# First character lowercasetext="HELLO WORLD"echo "${text,}" # hELLO WORLD
# All characters lowercaseecho "${text,,}" # hello worldWord Manipulation
Section titled “Word Manipulation”#!/usr/bin/env bashsentence="the quick brown fox jumps over the lazy dog"
# Remove first wordecho "${sentence#* }" # quick brown fox jumps over the lazy dog
# Remove last wordecho "${sentence% *}" # the quick brown fox jumps over the lazy
# Get first wordecho "${sentence%% *}" # the
# Get last wordecho "${sentence##* }" # dogPractical Examples
Section titled “Practical Examples”Example 1: Parse Log File Name
Section titled “Example 1: Parse Log File Name”#!/usr/bin/env bashlogfile="application-2024-01-15.log"
# Extract componentsfilename="${logfile%.*}" # application-2024-01-15extension="${logfile##*.}" # logdate_part="${filename#*-}" # 2024-01-15.log (after removing prefix)base_name="${filename%-*}" # application
echo "Log file: $logfile"echo "Base name: $base_name"echo "Date: $date_part"echo "Extension: $extension"Example 2: Extract Version from String
Section titled “Example 2: Extract Version from String”#!/usr/bin/env bash# Parse various version formatsversion_str="app-v1.2.3-beta"
# Extract version numberversion="${version_str##*-}" # beta (if using different pattern)version=$(echo "$version_str" | grep -oP '\d+\.\d+\.\d+' || echo "unknown")echo "Version: $version"
# Alternative: remove all non-numericclean_version="${version_str//[^0-9.]/}"echo "Clean: $clean_version"Example 3: Sanitize Input
Section titled “Example 3: Sanitize Input”#!/usr/bin/env bash# Remove special charactersinput="user@domain.com!#$%^&*()"
# Keep only alphanumericsanitized="${input//[^a-zA-Z0-9]/}"echo "Alphanumeric: $sanitized"
# Remove spacesno_spaces="${input// /}"echo "No spaces: $no_spaces"
# Trim whitespacetrimmed=$(echo "$input" | xargs)echo "Trimmed: $trimmed"Example 4: URL Parsing
Section titled “Example 4: URL Parsing”#!/usr/bin/env bashurl="https://example.com:8080/path/to/resource?key=value&foo=bar"
# Extract protocolprotocol="${url%%://*}"echo "Protocol: $protocol"
# Extract host and portremain="${url#*://}"host_port="${remain%%/*}"echo "Host:Port: $host_port"
# Extract pathpath="/${remain#*/}"path="${path%%\?*}"echo "Path: $path"
# Extract query stringquery="${remain##*\?}"echo "Query: $query"Example 5: String Padding
Section titled “Example 5: String Padding”#!/usr/bin/env bash# Left pad with zerosnum=42printf "%05d\n" "$num" # 00042
# Right pad with spacesprintf "%-10s\n" "hello" # hello
# Using bash parameter expansion (limited)text="hello"echo "${text}-------" # hello-------Example 6: Split String
Section titled “Example 6: Split String”#!/usr/bin/env bash# Split by delimiterIFS=',' read -ra parts <<< "apple,banana,cherry,date"
echo "Parts: ${#parts[@]}"for part in "${parts[@]}"; do echo " - $part"done
# Split by whitespaceread -ra words <<< "one two three four"echo "Words: ${words[@]}"External Tools for String Manipulation
Section titled “External Tools for String Manipulation”Using sed
Section titled “Using sed”#!/usr/bin/env bashtext="The quick brown fox jumps over the lazy dog"
# Replace first occurrenceecho "$text" | sed 's/brown/red/'
# Replace allecho "$text" | sed 's/the/a/g'
# Delete patternecho "$text" | sed '/quick/d'
# Extract by patternecho "$text" | sed -n 's/.*\(quick\).*/\1/p'Using awk
Section titled “Using awk”#!/usr/bin/env bashtext="apple,banana,cherry"
# Split and print fieldecho "$text" | awk -F',' '{print $2}'
# Print all fieldsecho "$text" | awk -F',' '{for(i=1;i<=NF;i++) print $i}'
# String lengthecho "hello" | awk '{print length($0)}'Advanced Operations
Section titled “Advanced Operations”Regular Expression Matching
Section titled “Regular Expression Matching”#!/usr/bin/env bashemail="user@example.com"
# Using [[ ]] =~ operatorif [[ "$email" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then echo "Valid email"else echo "Invalid email"fi
# Extract matchif [[ "$email" =~ ^([a-zA-Z0-9._%+-]+)@ ]]; then echo "Username: ${BASH_REMATCH[1]}"fiBase64 Encoding/Decoding
Section titled “Base64 Encoding/Decoding”#!/usr/bin/env bash# Encodeecho "Hello World" | base64# Output: SGVsbG8gV29ybGQK
# Decodeecho "SGVsbG8gV29ybGQK" | base64 -d
# File encodingbase64 input.txt -o output.b64base64 -d output.b64 -o decoded.txtSummary
Section titled “Summary”In this chapter, you learned about:
- ✅ String length and concatenation
- ✅ Substring extraction
- ✅ Pattern matching and removal
- ✅ String replacement
- ✅ Case conversion
- ✅ Word manipulation
- ✅ Practical DevOps examples
- ✅ External tools (sed, awk)
- ✅ Regular expression matching
- ✅ Base64 encoding/decoding
Exercises
Section titled “Exercises”Level 1: Basics
Section titled “Level 1: Basics”- Get the length of a string
- Extract substring from a string
- Convert string to uppercase
Level 2: Intermediate
Section titled “Level 2: Intermediate”- Parse a log filename to extract components
- Validate an email address using regex
- Parse a URL into components
Level 3: Advanced
Section titled “Level 3: Advanced”- Implement a string padding function
- Create a CSV parser
- Build a URL encoder/decoder
Next Steps
Section titled “Next Steps”Continue to the next chapter to learn about input/output operations.
Previous Chapter: Associative Arrays Next Chapter: Input/Output