π€ 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 :
