tech katta

Text processing for logs: sort, uniq, wc, cut, sed and awk

Lesson 06 of 23 · 4 min readFree

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
  1. Why this matters
  2. wc: count
  3. cut: take columns
  4. sort and uniq: order and count
  5. awk: fields, conditions and sums
  6. sed: find and replace in a stream
  7. Putting it together
  8. Try it

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:

Text
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.

Shell
wc -l access.log
wc -c access.log
Text
121 access.log
12221 access.log

Combined 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.

Shell
cut -d' ' -f1 access.log | head -3
cut -d'"' -f2 access.log | head -3
Text
198.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.1

Splitting 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:

Shell
printf '10\n9\n100\n' | sort
printf '10\n9\n100\n' | sort -n
Text
10
100
9
9
10
100

As 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.

Shell
printf 'b\na\nb\n' | uniq
printf 'b\na\nb\n' | sort | uniq -c
Text
b
a
b
      1 a
      2 b

Without 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.

Shell
awk '{print $9}' access.log | sort | uniq -c | sort -rn
Text
     92 200
     11 404
     11 304
      7 500

That 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?

Shell
awk '{print $7}' access.log | sort | uniq -c | sort -rn | head -5
Text
     29 /api/v1/courses
     25 /api/v1/lessons/12
     24 /health
     12 /api/v1/lessons/99
     11 /static/app.js

Which paths are returning server errors? Put a condition before the action: status code (field 9) of 500 or more.

Shell
awk '$9 >= 500 {print $7}' access.log | sort | uniq -c | sort -rn
Text
      7 /api/v1/lessons/99

All 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:

Shell
awk '$9==500 {print $4, $7}' access.log | head -3
Text
[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/99

awk 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:

Shell
awk '{b[$1]+=$10} END {for (i in b) print i, b[i]}' access.log | sort -k2 -rn | head -3
Text
198.51.100.9 148023
203.0.113.25 134923
203.0.113.10 112756

A final example computes a percentage:

Shell
awk '{t++; if ($9>=500) e++} END {printf "%d of %d requests were 5xx (%.1f%%)\n", e, t, 100*e/t}' access.log
Text
7 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/.

Shell
echo 'PORT=5000' | sed 's/5000/5100/'
Text
PORT=5100

A 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 -n to 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/' file edits the file in place, with no undo. Use -i.bak to keep a backup copy first.
Shell
sed -i.bak 's/client/cli/' a2.log
ls a2.log*
Text
a2.log
a2.log.bak

If 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:

Shell
awk '$9==404 {print $1}' access.log | sort | uniq -c | sort -rn | head -3

Each 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#

  1. Create a small file of ten lines with the format above (change the status codes). Use wc -l to confirm the count.
  2. Print only the client address and status code of each request with awk '{print $1, $9}'.
  3. Build the status code breakdown with awk, sort, uniq -c and sort -rn, then repeat it for the path field.
  4. Use awk to count how many responses were larger than a size you choose, for example more than 5000 bytes in field 10.
  5. Copy a file with cp, then use sed -i.bak 's/old/new/' on the copy and compare the two files with diff.