πŸ” Linux/Unix: Searching, Text Processing & Regex
Estimated reading: 3 minutes 273 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 :
Share

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

Or Copy Link

CONTENTS
Scroll to Top