Advanced String Operations in Bash
In this final step, we will explore more advanced string operations in Bash, including string manipulation, pattern matching, and substitution. These techniques will allow you to perform complex text processing tasks in your scripts.
String Manipulation Functions
Bash provides several built-in mechanisms for manipulating strings. Let's explore these operations:
-
Open the file string_ops.sh
in the editor:
- Navigate to
~/project/string_formatting
in the file explorer
- Click on
string_ops.sh
to open it
-
Replace the contents with the following code:
#!/bin/bash
## String length
message="Hello, Bash scripting world!"
echo "Original message: $message"
echo "Length of message: ${#message} characters"
## Extracting substrings
## Syntax: ${string:position:length}
echo "First 5 characters: ${message:0:5}"
echo "Characters 7-11: ${message:7:5}"
echo "From position 13 to end: ${message:13}"
## Searching and replacing within strings
filename="report.txt.backup"
echo "Original filename: $filename"
## Replace first occurrence
echo "Replace first occurrence of 'txt' with 'pdf': ${filename/txt/pdf}"
## Replace all occurrences
text="one two one two three"
echo "Original text: $text"
echo "Replace all occurrences of 'one' with '1': ${text//one/1}"
## Replace at beginning of string
echo "Replace 'one' at beginning: ${text/#one/1}"
## Replace at end of string
echo "Replace 'three' at end: ${text/%three/3}"
-
Save the file by pressing Ctrl+S
or selecting "File" > "Save" from the menu
-
Now let's make the file executable and run it:
chmod +x string_ops.sh
./string_ops.sh
You should see output showing various string manipulation operations:
Original message: Hello, Bash scripting world!
Length of message: 29 characters
First 5 characters: Hello
Characters 7-11: Bash
From position 13 to end: scripting world!
Original filename: report.txt.backup
Replace first occurrence of 'txt' with 'pdf': report.pdf.backup
Original text: one two one two three
Replace all occurrences of 'one' with '1': 1 two 1 two three
Replace 'one' at beginning: 1 two one two three
Replace 'three' at end: one two one two 3
Case Conversion
Bash also provides mechanisms for changing the case of text. Let's add case conversion examples:
- Add the following code to the end of
string_ops.sh
:
## Case conversion
uppercase="HELLO WORLD"
lowercase="hello world"
mixed="Hello World"
## Converting to uppercase
echo "Original: $lowercase"
echo "Uppercase: ${lowercase^^}"
## Converting to lowercase
echo "Original: $uppercase"
echo "Lowercase: ${uppercase,,}"
## Converting first character to uppercase
echo "Original: $lowercase"
echo "First character uppercase: ${lowercase^}"
## Converting first character to lowercase
echo "Original: $mixed"
echo "First character lowercase: ${mixed,}"
- Save the file and run it again:
./string_ops.sh
The additional output should show case conversion examples:
Original: hello world
Uppercase: HELLO WORLD
Original: HELLO WORLD
Lowercase: hello world
Original: hello world
First character uppercase: Hello world
Original: Hello World
First character lowercase: hello World
Let's add examples of string trimming and formatting techniques:
- Add the following code to the end of
string_ops.sh
:
## String trimming
path=" /usr/local/bin/ "
echo "Original path with spaces: '$path'"
echo "Trimmed path: '${path// /}'" ## Remove all spaces
## Removing prefixes and suffixes
filename="archive.tar.gz"
echo "Original filename: $filename"
echo "Without .tar.gz extension: ${filename%.tar.gz}"
echo "Without any extension: ${filename%%.*}"
echo "Extract extension: ${filename#*.}"
echo "Extract full extension: ${filename##*.}"
## Padding strings
number=42
printf "Number with leading zeros (5 digits): %05d\n" $number
name="Bob"
printf "Name padded to 10 characters: %-10s|\n" $name
- Save the file and run it again:
./string_ops.sh
The additional output should show trimming and formatting examples:
Original path with spaces: ' /usr/local/bin/ '
Trimmed path: '/usr/local/bin/'
Original filename: archive.tar.gz
Without .tar.gz extension: archive
Without any extension: archive
Extract extension: tar.gz
Extract full extension: gz
Number with leading zeros (5 digits): 00042
Name padded to 10 characters: Bob |
Practical Example: Log Parser
Let's create a practical example that uses advanced string operations to parse and format log entries:
- Add the following code to the end of
string_ops.sh
:
## Practical example: Log parser
echo -e "\n--- Log Parser Example ---\n"
## Sample log entries
log_entries=(
"[2023-10-25 08:15:22] INFO: Application started"
"[2023-10-25 08:16:45] WARNING: High memory usage detected"
"[2023-10-25 08:17:30] ERROR: Database connection failed"
"[2023-10-25 08:18:10] INFO: Backup process initiated"
"[2023-10-25 08:19:55] ERROR: Out of disk space"
)
## Parse and format log entries
for entry in "${log_entries[@]}"; do
## Extract timestamp (everything between [ and ])
timestamp=${entry#*[}
timestamp=${timestamp%%]*}
## Extract log level (INFO, WARNING, ERROR)
level=${entry#*] }
level=${level%%:*}
## Extract message
message=${entry#*: }
## Format based on log level
case $level in
"INFO")
printf "%-20s [\033[36m%-7s\033[0m] %s\n" "$timestamp" "$level" "$message"
;;
"WARNING")
printf "%-20s [\033[33m%-7s\033[0m] %s\n" "$timestamp" "$level" "$message"
;;
"ERROR")
printf "%-20s [\033[31m%-7s\033[0m] %s\n" "$timestamp" "$level" "$message"
;;
esac
done
- Save the file and run it:
./string_ops.sh
The final output section should show formatted log entries with different colors based on log level:
--- Log Parser Example ---
2023-10-25 08:15:22 [INFO ] Application started
2023-10-25 08:16:45 [WARNING] High memory usage detected
2023-10-25 08:17:30 [ERROR ] Database connection failed
2023-10-25 08:18:10 [INFO ] Backup process initiated
2023-10-25 08:19:55 [ERROR ] Out of disk space
This log parser example demonstrates how combining various string operations with formatting and color codes can transform raw text data into a more readable and structured format. Such techniques are extremely useful when developing scripts for log analysis, data processing, or reporting.