The Plain Picture#
The machines on the factory line from Chapter 5: a sieve (keep only certain lines), a sorter, a counter, a slicer (keep only certain columns), a find-and-replace stamp.
The Real Names#
| Tool | Job | Example |
|---|---|---|
grep | keep lines matching a pattern | grep "ERROR" app.log |
sort | sort lines | sort -n (numbers), -r (reverse), -u (unique) |
uniq | collapse repeated adjacent lines; -c counts them | sort | uniq -c |
cut | take columns by a separator | cut -d',' -f2 data.csv |
awk | a small language for column-based text | awk '{print $1}' (first column) |
sed | find-and-replace in a stream | sed 's/http:/https:/g' |
find | search for files by name, type, size, age | find /var/log -name "*.log" -mtime +7 |
xargs | turn input lines into arguments of another command | find . -name "*.tmp" | xargs rm |
jq | pick apart JSON (Networking 3: DNS, HTTP and HTTPS Ch 6) | curl -s api/notes | jq '.[0].title' |
grep options you'll use daily#
grep -i "error" app.log # ignore upper/lower case
grep -n "error" app.log # show line numbers
grep -r "DATABASE_URL" . # search every file under this folder (recursive)
grep -v "healthcheck" app.log # INVERT: lines that do NOT match
grep -E "401|403" app.log # extended patterns: 401 OR 403
grep -A 5 "Traceback" app.log # also show 5 lines After each match (-B = before)The patterns grep, sed and awk use are regular expressions (regex) — a mini-language for text patterns:
. any character, * repeat the previous thing zero or more times, ^ start of line, $ end of line, [0-9] any digit.
^ERROR = lines starting with ERROR.
Under the Hood — a real log analysis#
StudyBuddy's web server writes one line per request. A sample access.log:
49.207.1.10 - - [28/Sep/2026:10:00:01 +0530] "GET /notes/priya HTTP/1.1" 200 1532
103.21.4.7 - - [28/Sep/2026:10:00:02 +0530] "POST /login HTTP/1.1" 401 52
49.207.1.10 - - [28/Sep/2026:10:00:03 +0530] "GET /quiz/12 HTTP/1.1" 200 877
157.48.9.3 - - [28/Sep/2026:10:00:03 +0530] "GET /notes/rahul HTTP/1.1" 500 21
103.21.4.7 - - [28/Sep/2026:10:00:04 +0530] "POST /login HTTP/1.1" 401 52
103.21.4.7 - - [28/Sep/2026:10:00:05 +0530] "POST /login HTTP/1.1" 401 52
49.207.1.10 - - [28/Sep/2026:10:00:06 +0530] "GET /notes/priya HTTP/1.1" 304 0
157.48.9.3 - - [28/Sep/2026:10:00:07 +0530] "GET /notes/rahul HTTP/1.1" 500 21awk splits each line on spaces, numbering the fields $1, $2, …: $1 is the client IP and $9 is the status code
(Networking 3: DNS, HTTP and HTTPS Ch 5).
"Which IPs send the most requests?" — the classic interview question:
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -3Real output:
3 49.207.1.10
3 103.21.4.7
2 157.48.9.3Read the pipeline stage by stage:
awk '{print $1}'— keep only the IP column.sort— put identical IPs next to each other (uniqonly merges adjacent lines — that's whysortcomes first).uniq -c— collapse duplicates, with a count in front.sort -rn— sort by that count, as a number, in reverse (biggest first).head -3— keep the top 3.
"How many requests got each status code?"
awk '{print $9}' access.log | sort | uniq -c | sort -rn 3 401
2 500
2 200
1 304Three 401s from 103.21.4.7 in 3 seconds against /login looks like someone guessing passwords — that's how the
command line helps you spot an attack. Two 500s on /notes/rahul point to a specific bug.
More recipes:
grep '" 5[0-9][0-9] ' access.log | wc -l # count 5xx responses → 2
find /var/log -name "*.log" -size +100M # log files bigger than 100 MB
find . -name "*.pyc" -delete # delete all compiled Python files here
sed -i.bak 's/DEBUG=true/DEBUG=false/' .env # edit a file in place, keeping a .bak copy
curl -s https://api.github.com/repos/python/cpython | jq '.stargazers_count' # one field from JSONGotchas
uniqwithoutsortfirst gives wrong counts, because it only merges adjacent duplicates.sed -idiffers between macOS and Linux. On Linux,sed -i 's/a/b/' fworks; macOS needs an argument after-i(sed -i '' 's/a/b/' f). Giving a backup suffix like-i.bakworks on both.xargsbreaks on file names with spaces. Usefind ... -print0 | xargs -0 ..., orfind ... -exec cmd {} +.
Check yourself
4 questionsWrite a pipeline for "the 5 most common status codes in access.log".
Show answerHide answer
awk '{print $9}' access.log | sort | uniq -c | sort -rn | head -5.
Why must sort come before uniq -c?
Show answerHide answer
uniq only merges duplicates that are next to each other; sorting groups them.
How do you show the 3 lines before and after every "Traceback" in a log?
Show answerHide answer
grep -B 3 -A 3 "Traceback" app.log (or grep -C 3).
How do you find files under /tmp older than 7 days?
Show answerHide answer
find /tmp -type f -mtime +7.