Linux/Unix: Regex Concepts & Practice β Mastering Pattern Matching in Shell
Introduction β Why Learn Regular Expressions (Regex) in Linux?
Regular expressions (regex) are the heart of text searching, filtering, and data validation in Linux/Unix. Whether you’re using grep, sed, awk, or scripting in Bash, regex helps you create powerful, flexible patterns for matching strings and structures in files, logs, and outputs.
In this guide, youβll learn:
- Key regex symbols and their meanings
- Practical examples with tools like
grep,sed, andawk - Real-world usage for filtering logs, validating input, and processing data
Basic Regex Syntax & Meaning
| Symbol | Meaning | Example | Matches |
|---|---|---|---|
. | Any single character | a.c | abc, a1c, a-c |
* | Zero or more of the preceding char | lo* | l, lo, loo, looo |
+ | One or more of the preceding char | go+ | go, goo, gooo |
? | Zero or one occurrence | colou?r | color, colour |
[] | Match any one character in brackets | [aeiou] | a, e, i, o, u |
[^] | Match any character NOT in brackets | [^0-9] | Any non-digit character |
^ | Start of a line | ^root | Lines starting with root |
$ | End of a line | .com$ | Lines ending in .com |
| ` | ` | OR pattern | `cat |
() | Group patterns | (ab)+ | ab, abab, ababab |
{} | Quantifier for repetitions | a{2,4} | aa, aaa, aaaa |
Using Regex with grep, egrep, awk, and sed
1. grep β Basic Pattern Search
Example: Match lines that start with “root”
grep "^root" /etc/passwd
Output:
root:x:0:0:root:/root:/bin/bash
2. egrep β Extended Regex Support
Example: Match either “cat” or “dog”
egrep "cat|dog" animals.txt
Output:
I love my dog.
The cat is sleeping.
3. awk β Pattern Matching by Fields
Example: Match rows where the second column is a number
awk '$2 ~ /^[0-9]+$/' data.txt
Output:
user1 42
admin 103
4. sed β Stream Editing with Regex
Example: Replace lines ending with .html to .php
sed 's/\.html$/\.php/' urls.txt
Output:
index.php
about.php
Practice Regex β Real-World Scenarios
Find all lines with a 3-digit number
grep -E "[0-9]{3}" data.txt
Extract valid email addresses
grep -E "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-z]{2,}" file.txt
Replace multiple spaces with one space
sed 's/ */ /g' messy.txt
Validate phone number format (XXX-XXX-XXXX)
grep -E "^[0-9]{3}-[0-9]{3}-[0-9]{4}$" phones.txt
Output:
123-456-7890
555-000-1111
Summary β Recap & Next Steps
Regex enables dynamic pattern matching and text transformation in Linux. From validating formats to filtering logs, it’s a crucial skill for any sysadmin or developer.
Key Takeaways:
- Learn key regex symbols:
^,$,*,+,?,{},[],(),| - Use
grepfor search,sedfor substitution, andawkfor field-aware pattern matching - Practice regex to handle complex data parsing tasks
FAQs
Whatβs the difference between grep and egrep?
egrep supports extended regex like +, ?, | without escaping. grep requires escaping them.
How do I match an exact word using regex?
Use:
grep -w "word" file.txt
Can regex be used inside shell scripts?
Absolutely! [[ "$var" =~ pattern ]] is valid in Bash.
How do I test regex interactively?
Use tools like grep, sed, or online playgrounds like regex101.com.
Can I replace only matching groups in sed?
Yes, with \1, \2 etc. Example:
sed -E 's/(.*)\.html/\1.php/' file.txt
Share Now :
