Linux/Unix: Searching, Text Processing & Regex β grep, find, sed, awk, and Pattern Matching
Introduction β Supercharge Your Shell with Search & Pattern Tools
In Linux/Unix, the real power of the command line lies in text processing and file searching. Whether youβre filtering logs, extracting data, or matching patterns with regular expressions, tools like grep, find, awk, and sed turn simple commands into data-processing powerhouses.
In this guide, youβll learn:
- How to search inside files with
grep,fgrep, andegrep - How to locate files with
find,locate,which, andwhereis - How to manipulate structured data using
awkandsed - How to write and apply regular expressions for powerful matching
Topics Covered
| Subtopic | Description |
|---|---|
Text Search (grep, fgrep, egrep) | Search file content for strings or patterns |
File Location (find, locate, which) | Find files by name, path, or type |
Text Processing (awk, sed) | Extract and modify fields, lines, or characters in files |
| Regex Concepts | Learn how regular expressions work and how to use them effectively |
Linux/Unix: Text Search (grep, fgrep, egrep)
grep β Search Lines for a Pattern
grep "error" logfile.txt
Displays lines containing βerrorβ.
-i, -n, and -r Options
grep -i "warning" logfile.txt # Case-insensitive
grep -n "fail" logfile.txt # Show line numbers
grep -r "config" /etc # Recursive directory search
fgrep β Literal Search (No Regex)
fgrep "a.b" file.txt
Searches for exact string a.b, not regex.
egrep β Extended Regex Support
egrep "error|fail" logs.txt
Matches either βerrorβ or βfailβ.
Linux/Unix: File Location (find, locate, which, whereis)
find β Search Files by Name, Type, Time
find /home -name "*.txt"
Finds all .txt files recursively.
find /etc -type f -size +1M
Finds files over 1MB.
locate β Fast Filename Search (uses database)
locate passwd
Run updatedb to refresh the database.
which & whereis β Command Location
which python3 # Shows full path
whereis python3 # Shows binary, source, man
Linux/Unix: Text Processing (awk, sed)
awk β Process Fields in Files
awk '{print $1, $3}' data.txt
Prints the first and third columns.
awk -F: '{print $1}' /etc/passwd
Uses : as a field separator.
sed β Stream Editor for Replacements
sed 's/error/ERROR/g' logfile.txt
Replaces βerrorβ with βERRORβ in every line.
sed -n '2,4p' file.txt
Prints only lines 2 through 4.
Linux/Unix: Regex Concepts & Practice
Basic Metacharacters
| Pattern | Meaning |
|---|---|
. | Any single character |
* | Zero or more of previous |
^ | Start of line |
$ | End of line |
[] | Match character set |
| ` | ` |
Regex Example with grep -E
grep -E "^[A-Z].*\.log$" files.txt
Matches lines that start with a capital letter and end in .log.
Practice Regex with grep or sed
grep -E "user[0-9]{2}" names.txt
sed -E 's/[0-9]+/NUM/' numbers.txt
Summary β Recap & Next Steps
The tools in this guide are the foundation of shell data manipulation. When combined, they allow for automated parsing, dynamic reporting, and intelligent searchingβright from the terminal.
Key Takeaways:
- Use
grep,fgrep, andegrepfor file content filtering - Use
find,locate,whichto locate files or commands - Use
awkandsedto process fields, rows, and text replacements - Regular expressions let you match patterns and automate logic
Real-World Applications:
- Log parsing for DevOps and monitoring
- Data extraction and reporting from structured files
- Finding configuration files or scripts across directories
Frequently Asked Questions
Whatβs the difference between grep, fgrep, and egrep?
grep uses basic regex, fgrep searches fixed strings (no regex), and egrep allows extended regex.
How do I search all files for a keyword recursively?
Use:
grep -r "keyword" .
Can I use find to delete files?
Yes:
find . -name "*.log" -delete
How do I replace text in-place using sed?
Use:
sed -i 's/foo/bar/g' file.txt
Whatβs the best way to count unique values in a column?
Combine awk, sort, and uniq:
awk '{print $2}' data.txt | sort | uniq -c
Share Now :
