grep, sed & awk in Practice

💤0
Lv 10 XP
← ⚙️ Scripting & Automation · Regular Expressions

grep, sed & awk in Practice

Intermediate ⭐ 80 XP ⏱ 18 min #regex#grep#sed#awk

Apply regex with the classic text trio to search, transform, and report on data.

📖Theory

Three command-line tools turn regex into real work:

  • grepfind lines matching a pattern (-E extended regex, -i ignore case, -r recursive, -o only the match, -c count)
  • sededit streams, classically substitution: sed 's/old/new/g'
  • awkfield 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
  1. Use grep -oE to extract all IP addresses from a log file.
  2. Use sed to replace every tab with a comma in a file.
  3. Use awk to print the 1st and 3rd columns of a space-separated file.
  4. Combine grep + awk to sum a numeric column only for matching lines.
🧾Cheat Sheet
ToolCommon use
grep -E patfilter lines (extended regex)
grep -i / -r / -cignore case / recurse / count
grep -oE patprint only the matches
sed 's/a/b/g'substitute all on each line
sed -Eextended 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