π 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
awk
andsed
- 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
, andegrep
for file content filtering - Use
find
,locate
,which
to locate files or commands - Use
awk
andsed
to 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 :