Lesson 10 · tested on Debian 13
awk for people who hate awk: 8 one-liners that answer real questions about a log
awk looks cryptic, but most real uses follow one pattern: a condition, then an action in braces. Using a small web server log, this video builds eight practical one-liners: pick columns, filter errors, sum bytes, compute an average, count requests per IP, read CSV with -F, use $NF, and avoid the double-quote trap that makes awk print whole lines.
Video coming soon — @ShellProven
Commands
Exactly as run in the video. Full folder with setup script and log: terminal-tutorials/awk-log-one-liners.
# The log and the one idea
$ cat access.log
10.0.0.5 GET /index.html 200 5120 12
10.0.0.7 GET /login 200 2048 30
10.0.0.5 POST /login 302 512 45
10.0.0.9 GET /admin 404 256 3
10.0.0.7 GET /api/orders 500 128 950
10.0.0.5 GET /missing.png 404 256 2
10.0.0.9 GET /api/orders 200 8192 120
10.0.0.7 GET /index.html 200 5120 10
# 1. Pick columns
$ awk '{print $1, $4}' access.log
10.0.0.5 200
10.0.0.7 200
10.0.0.5 302
10.0.0.9 404
10.0.0.7 500
10.0.0.5 404
10.0.0.9 200
10.0.0.7 200
# 2. Filter lines with a condition
$ awk '$4 >= 400' access.log
10.0.0.9 GET /admin 404 256 3
10.0.0.7 GET /api/orders 500 128 950
10.0.0.5 GET /missing.png 404 256 2
$ awk '$4 == 404 {print $3}' access.log
/admin
/missing.png
# 3. Sum and average
$ awk '{sum += $5} END {print sum, "bytes"}' access.log
21632 bytes
$ awk '{t += $6; n++} END {printf "avg %.1f ms over %d requests\n", t/n, n}' access.log
avg 146.5 ms over 8 requests
# 4. Count per value
$ awk '{c[$1]++} END {for (ip in c) print c[ip], ip}' access.log | sort -rn
3 10.0.0.7
3 10.0.0.5
2 10.0.0.9
# 5. Other separators and the last field
$ awk -F, 'NR > 1 {print $2 " lives in " $3}' users.csv
Ana lives in Lisbon
Tom lives in Leeds
Mia lives in Graz
$ awk '$NF > 100 {print $NF " ms", $3}' access.log
950 ms /api/orders
120 ms /api/orders
# The double-quote trap
$ awk "{print $1}" access.log
10.0.0.5 GET /index.html 200 5120 12
10.0.0.7 GET /login 200 2048 30
10.0.0.5 POST /login 302 512 45
10.0.0.9 GET /admin 404 256 3
10.0.0.7 GET /api/orders 500 128 950
10.0.0.5 GET /missing.png 404 256 2
10.0.0.9 GET /api/orders 200 8192 120
10.0.0.7 GET /index.html 200 5120 10
Quick check
Same two questions as in the video. Checked here, in your browser — nothing is sent anywhere.
Transparency. Script and narration made with AI, human-reviewed. The robot and the dog are AI-generated cartoon characters. The commands and their output are real runs in a container.