Bash Regex - Pattern Matching with =~, grep, sed, and awk
Use regular expressions in bash with the =~ operator, grep patterns, sed substitution, and awk matching. Includes capture groups and common regex patterns.
Detailed Explanation
Regular Expressions in Bash
Bash supports regular expressions through the =~ operator in [[ ]] tests, and through external commands like grep, sed, and awk.
The =~ Operator
The =~ operator tests a string against a regex pattern:
# Basic match
if [[ "hello123" =~ [0-9]+ ]]; then
echo "Contains numbers"
fi
# Capture groups with BASH_REMATCH
VERSION="v3.14.2"
if [[ "$VERSION" =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
echo "Major: ${BASH_REMATCH[1]}" # 3
echo "Minor: ${BASH_REMATCH[2]}" # 14
echo "Patch: ${BASH_REMATCH[3]}" # 2
fi
# Negated match
if [[ ! "$input" =~ ^[0-9]+$ ]]; then
echo "Not a number"
fi
Common Validation Patterns
# Email validation
[[ "$email" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]
# IPv4 address
[[ "$ip" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]
# Semantic version
[[ "$ver" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]
# URL
[[ "$url" =~ ^https?:// ]]
# Date (YYYY-MM-DD)
[[ "$date" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]
grep with Regex
# Extended regex (-E)
grep -E "error|warning|fatal" app.log
# Match IP addresses
grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}" access.log
# Match email addresses
grep -oE "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}" contacts.txt
# Perl-compatible regex (-P)
grep -P "\d{3}-\d{2}-\d{4}" data.txt
sed with Regex
# Capture group back-references
echo "John Smith" | sed -E 's/^(\w+) (\w+)$/\2, \1/'
# Output: Smith, John
# Remove HTML tags
sed 's/<[^>]*>//g' page.html
# Extract between delimiters
echo "key=value" | sed 's/.*=//'
awk with Regex
# Match lines with a pattern
awk '/ERROR/ {print $0}' app.log
# Match a specific field
awk -F',' '$3 ~ /^[0-9]+$/ {print $1, $3}' data.csv
# Substitute in a field
awk '{gsub(/old/, "new", $2); print}' data.txt
Use Case
Regular expressions in bash are used for input validation (emails, IP addresses, dates), log file parsing (extracting timestamps, IPs, error codes), data transformation (reformatting fields, extracting values), and configuration management (searching and replacing patterns across files).
Try It — Bash Cheat Sheet
Related Topics
Bash String Manipulation - Substring, Replace, Trim, Case
String Manipulation
Bash Text Processing - grep, sed, awk, sort, cut
Text Processing
Bash If/Else Conditions - Tests, Comparisons, and File Checks
Control Flow
Bash For Loops - Iterating Over Lists, Ranges, and Files
Control Flow
Bash Script Basics - Shebang, Arguments, Exit Codes, set Options
Script Basics