πŸ” Linux/Unix: Searching, Text Processing & Regex
Estimated reading: 3 minutes 22 views

πŸ”€ 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, and awk
  • Real-world usage for filtering logs, validating input, and processing data

πŸ”’ Basic Regex Syntax & Meaning

SymbolMeaningExampleMatches
.Any single charactera.cabc, a1c, a-c
*Zero or more of the preceding charlo*l, lo, loo, looo
+One or more of the preceding chargo+go, goo, gooo
?Zero or one occurrencecolou?rcolor, 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^rootLines starting with root
$End of a line.com$Lines ending in .com
``OR pattern`cat
()Group patterns(ab)+ab, abab, ababab
{}Quantifier for repetitionsa{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 grep for search, sed for substitution, and awk for 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ”΅ Linux/Unix: Regex Concepts & Practice

Or Copy Link

CONTENTS
Scroll to Top