Text processing for logs: sort, uniq, wc, cut, sed and awk
In this lesson
Turn a raw log file into answers: count status codes, find the busiest paths and clients, and clean or edit text from the command line.
On this page
A log file is just lines of text, and the commands in this lesson turn those lines into answers. With a handful of them you can say how many requests failed, which URL is slowest to respond or which client address is hammering the server, without installing anything.
Why this matters#
When an alert says "error rate is up", you want the answer in a minute, not after setting up a dashboard. On the server you are logged into, awk, sort and uniq give you that answer right away. They also appear in nearly every runbook and shell script you will inherit, so reading them fluently is part of the job.
The examples use the 121-line access.log from lesson 4. Each line looks like this:
198.51.100.9 - - [02/Dec/2026:10:00:12 +0000] "GET /api/v1/lessons/99 HTTP/1.1" 500 3175 "-" "curl/8.5.0"Split on spaces, field 1 is the client address, field 4 the time, field 6 the method, field 7 the path, field 9 the status code and field 10 the response size. Remember those numbers; they come up again and again.
wc: count#
wc counts lines, words and bytes. -l is the one you will use most.
wc -l access.log
wc -c access.log121 access.log
12221 access.logCombined with grep -c or a pipe, it counts anything: grep ' 500 ' access.log | wc -l.
cut: take columns#
cut extracts parts of each line. -d sets the delimiter and -f picks the fields.
cut -d' ' -f1 access.log | head -3
cut -d'"' -f2 access.log | head -3198.51.100.4
198.51.100.9
198.51.100.9
POST /login HTTP/1.1
GET /api/v1/lessons/99 HTTP/1.1
GET /static/app.js HTTP/1.1Splitting on a space gives the address. Splitting on a double quote gives the request line, because it sits between the first two quotes. cut is simple and fast, but it treats every single space as a separator, so a field that has extra spaces breaks it. For irregular text, use awk.
On Linux, files such as /etc/passwd are colon-separated, which suits cut well: cut -d: -f1,7 /etc/passwd prints each user name and its login shell.
sort and uniq: order and count#
sort orders lines. By default it compares as text, which surprises people with numbers:
printf '10\n9\n100\n' | sort
printf '10\n9\n100\n' | sort -n10
100
9
9
10
100As text, 10 comes before 9 because 1 sorts before 9. Use -n for numbers, -r to reverse and -u to drop duplicates. -k2 sorts by the second field.
uniq collapses repeated lines, but only when they are next to each other. That is why it nearly always follows sort. uniq -c adds a count.
printf 'b\na\nb\n' | uniq
printf 'b\na\nb\n' | sort | uniq -cb
a
b
1 a
2 bWithout sorting first, the two b lines were not adjacent, so uniq did not merge them. Sorting then counting is a pattern worth memorising: sort | uniq -c | sort -rn, the last sort ranking by count, highest first.
awk '{print $9}' access.log | sort | uniq -c | sort -rn 92 200
11 404
11 304
7 500That is the status code breakdown for the whole log in one line. 92 requests succeeded, 7 returned a server error. The two 404 and 304 rows tie on 11; their order is not guaranteed.
awk: fields, conditions and sums#
awk reads each line, splits it into fields on whitespace ($1, $2, and so on; $0 is the whole line) and runs the code you give it. Its basic shape is awk 'condition { action }'. Leave out the condition and the action runs on every line. Leave out the action and matching lines are printed.
Which paths are requested most?
awk '{print $7}' access.log | sort | uniq -c | sort -rn | head -5 29 /api/v1/courses
25 /api/v1/lessons/12
24 /health
12 /api/v1/lessons/99
11 /static/app.jsWhich paths are returning server errors? Put a condition before the action: status code (field 9) of 500 or more.
awk '$9 >= 500 {print $7}' access.log | sort | uniq -c | sort -rn 7 /api/v1/lessons/99All seven failures come from one endpoint. That is a real lead: look at the code behind /api/v1/lessons/99, not at the whole server.
Print several fields at once and format them:
awk '$9==500 {print $4, $7}' access.log | head -3[02/Dec/2026:10:00:12 /api/v1/lessons/99
[02/Dec/2026:10:02:13 /api/v1/lessons/99
[02/Dec/2026:10:02:27 /api/v1/lessons/99awk can also add things up. The pattern array[key] += value builds totals, and an END block runs after the last line. Total response bytes sent to each client:
awk '{b[$1]+=$10} END {for (i in b) print i, b[i]}' access.log | sort -k2 -rn | head -3198.51.100.9 148023
203.0.113.25 134923
203.0.113.10 112756A final example computes a percentage:
awk '{t++; if ($9>=500) e++} END {printf "%d of %d requests were 5xx (%.1f%%)\n", e, t, 100*e/t}' access.log7 of 121 requests were 5xx (5.8%)Use -F to change the separator: awk -F'"' '{print $2}' access.log splits on quotes. When a condition compares fields, remember that $9 >= 500 compares as a number when the field looks like one.
sed: find and replace in a stream#
sed edits text as it flows past. Its most common job is substitution, written s/old/new/.
echo 'PORT=5000' | sed 's/5000/5100/'PORT=5100A few more forms:
sed 's/a/b/g'replaces every match on the line, not just the first.sed -n '2,3p'prints only lines 2 to 3, using-nto turn off the default printing.sed '/health/d'deletes lines that match a pattern. Run on the log, it removed 24 health-check lines and left 97.sed -i 's/a/b/' fileedits the file in place, with no undo. Use-i.bakto keep a backup copy first.
sed -i.bak 's/client/cli/' a2.log
ls a2.log*a2.log
a2.log.bakIf your pattern contains slashes, such as a path, pick another separator: sed 's|/old/path|/new/path|'. For anything involving structured data like JSON or YAML, prefer a tool that understands the format; sed only sees text.
Putting it together#
Which client addresses are generating the most 404 responses? Chain what you have learned:
awk '$9==404 {print $1}' access.log | sort | uniq -c | sort -rn | head -3Each stage does one small step, and you can build the line by running the pipeline one piece at a time and checking the output after each |. That habit is the secret to writing these without mistakes.
Try it#
- Create a small file of ten lines with the format above (change the status codes). Use
wc -lto confirm the count. - Print only the client address and status code of each request with
awk '{print $1, $9}'. - Build the status code breakdown with
awk,sort,uniq -candsort -rn, then repeat it for the path field. - Use
awkto count how many responses were larger than a size you choose, for example more than 5000 bytes in field 10. - Copy a file with
cp, then usesed -i.bak 's/old/new/'on the copy and compare the two files withdiff.
Sign in to keep track of the lessons you finish.