AI disclosure: this article was created with the help of AI. An AI assistant drafted the text from the lesson script, and it was reviewed before publishing. Every command was actually run in a fresh debian:13 container (mawk 1.3.4 (Debian's default awk; the same one-liners work in gawk)); all outputs below are copied from that run, not written by hand.
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.
The problem: Most people copy awk commands from the internet without understanding them. That's a shame, because most everyday awk is built on one simple idea.
The log and the one idea
Here's the log. Each line has six columns: client I P, method, path, status code, bytes sent, and response time in milliseconds.
$ 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
awk reads the file line by line and splits each line into fields: dollar one, dollar two, and so on. Each program is a condition followed by an action in curly braces. That's the whole idea.
1. Pick columns
With no condition, the action runs on every line. Let's print the I P and the status code.
$ 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
The comma puts a space between the fields. Always wrap the program in single quotes, we'll see why at the end.
2. Filter lines with a condition
With no action, awk prints the lines that match the condition. Here: every request with a status of four hundred or more.
$ 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
Unlike grep, this compares numbers in one specific column, so a four hundred somewhere else in the line can't fool it. Condition and action together: the paths that returned not found.
$ awk '$4 == 404 {print $3}' access.log
/admin
/missing.png
3. Sum and average
END runs once, after the last line. That's where you print totals. How many bytes did we send?
$ awk '{sum += $5} END {print sum, "bytes"}' access.log
21632 bytes
Variables start at zero, so there's nothing to declare. For an average, count the lines as well. printf formats the number.
$ 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
Who sends the most requests? An array indexed by I P counts each one, and the end block prints the counts.
$ 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
awk doesn't promise any order in that loop, so we sort the result afterwards. Two clients are tied with three requests each.
5. Other separators and the last field
By default awk splits on spaces. For CSV, set the separator with -F,. NR is the line number, so NR > 1 skips the header.
$ awk -F, 'NR > 1 {print $2 " lives in " $3}' users.csv
Ana lives in Lisbon
Tom lives in Leeds
Mia lives in Graz
Strings placed next to each other are simply joined. And $NF is always the last field, however many there are. Here: slow requests, over a hundred milliseconds.
$ awk '$NF > 100 {print $NF " ms", $3}' access.log
950 ms /api/orders
120 ms /api/orders
Both slow requests hit the orders A P I. That's where you'd start looking.
The double-quote trap
Finally, the mistake that makes people give up on awk. Let's use double quotes instead of single quotes.
$ 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
We asked for the first column and got whole lines. Inside double quotes, the shell replaces dollar one with its own, empty, variable, before awk ever runs. So awk receives just print, which prints the whole line. Use single quotes around awk programs, always.
The trap, in one paragraph
Which awk variable always holds the last field of a line? NF is the number of fields, so $NF is the last one.
Quick check
1. Why does awk "{print $1}" access.log print whole lines?
- awk does not accept double quotes
- The shell replaced $1 inside the double quotes
- $1 means the whole line
Answer
The shell replaced $1 inside the double quotes: Bash swapped dollar one for its own empty variable before awk ran. Use single quotes around awk programs.
2. Which awk variable always holds the last field of a line?
- $0
- $NF
- NR
Answer
$NF: NF is the number of fields, so $NF is the last one.
Recap
An awk program is a condition and an action. Fields are dollar one, dollar two, up to $NF. No action prints matching lines, and the end block prints totals. Arrays count things, -F changes the separator, NR is the line number, and the program goes in single quotes.
Source and reproducible setup
- Lesson page with the same quiz (and a video version): https://shellprovenai.github.io/lekcje/awk-log-one-liners/
- Commands, demo setup script and full test log: https://github.com/shellprovenai/terminal-tutorials/tree/main/awk-log-one-liners
This article was created with the help of AI and reviewed before publishing. The video on the lesson page uses AI narration and AI-generated robot-and-dog presenters. No sponsors, no affiliate links.
Top comments (0)