Regular expressions (regex) are powerful tools for pattern matching and text manipulation in shell scripts. They allow you to search, match, and replace strings efficiently. In this article, we will explore three practical examples of using regular expressions in shell scripts that will help you automate tasks and improve data processing.
In many scenarios, you may need to extract specific information, such as email addresses, from a large text file. This example demonstrates how to use regular expressions to achieve that efficiently.
#!/bin/bash
# Define the input file
input_file="contacts.txt"
# Use grep with a regex pattern to extract email addresses
grep -oP '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}' "$input_file"
This script uses grep
with the -oP
options to extract and display only the email addresses found in contacts.txt
. The regex pattern matches valid email formats.
grep
with Perl-compatible regex support if your default version does not support the -P
option.>
to save the extracted emails.You may need to validate user input, such as checking if a string matches a specific pattern, like a phone number format. Here’s how to do this in a shell script.
#!/bin/bash
# Read user input
read -p "Enter your phone number (123-456-7890): " phone
# Validate the phone number format using regex
if [[ $phone =~ ^[0-9]{3}-[0-9]{3}-[0-9]{4}$ ]]; then
echo "Valid phone number"
else
echo "Invalid phone number format"
fi
This script prompts the user to enter a phone number and uses regex in a conditional statement to validate the format. If the input matches the specified pattern, it confirms validity; otherwise, it notifies the user.
XXX-XXX-XXXX
, where X
is a digit.In cases where you need to modify text within files, regular expressions can be incredibly useful. This example demonstrates how to replace all instances of a specific word or phrase.
#!/bin/bash
# Define the input file and word to replace
input_file="document.txt"
word_to_replace="old"
new_word="new"
# Use sed to replace the specified word with a new one
sed -i "s/\b$word_to_replace\b/$new_word/g" "$input_file"
This script uses sed
to search for the word old
in document.txt
and replaces it with new
. The -i
option edits the file in place, while \b
ensures that the script matches whole words only.
g
flag at the end of the sed
command signifies that all occurrences in the line will be replaced.-i
option.By leveraging these examples of using regular expressions in shell scripts, you can enhance your scripting capabilities and streamline various text processing tasks.