grep, sed & awk in Practice
Apply regex with the classic text trio to search, transform, and report on data.
Theory
Three command-line tools turn regex into real work:
- grep — find lines matching a pattern (
-Eextended regex,-iignore case,-rrecursive,-oonly the match,-ccount) - sed — edit streams, classically substitution:
sed 's/old/new/g' - awk — field processing and reporting, splitting each line into
$1, $2…
A rough rule: grep to filter, sed to transform, awk to compute. Combined in a pipe they answer almost any log/CSV question without writing a program.
Real-World Example
# grep: count 5xx responses in an access log
grep -cE ' 5[0-9]{2} ' access.log
# sed: redact emails in a file
sed -E 's/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+/[REDACTED]/g' users.txt
# awk: sum the bytes column (field 10) of an access log
awk '{ total += $10 } END { print total }' access.log
# awk: average response time, printing only slow requests
awk '$NF > 1.0 { print $1, $NF }' timings.log Hands-On Exercise
- Use
grep -oEto extract all IP addresses from a log file. - Use
sedto replace every tab with a comma in a file. - Use
awkto print the 1st and 3rd columns of a space-separated file. - Combine grep + awk to sum a numeric column only for matching lines.
Cheat Sheet▾
| Tool | Common use |
|---|---|
grep -E pat | filter lines (extended regex) |
grep -i / -r / -c | ignore case / recurse / count |
grep -oE pat | print only the matches |
sed 's/a/b/g' | substitute all on each line |
sed -E | extended regex in sed |
awk '{print $2}' | print a field |
awk '{s+=$1} END{print s}' | sum a column |
Common Interview Questions▾
When would you use awk instead of grep?
When you need to work with columns/fields or compute (sum, average, count by field). grep filters whole lines; awk splits each line into fields and runs logic.
How do you do a find-and-replace across a stream with sed?
sed 's/pattern/replacement/g' substitutes all occurrences per line; add -E for
extended regex and -i to edit files in place.
What does grep -E enable?
Extended regular expressions, so + ? | () work without backslash-escaping, giving
you modern regex syntax directly.
Official Documentation
📝 My notes on this topic
Auto-saves as you type