Find a course

Module 3: Redirection, Pipes and Text ProcessingLesson 8 of 1312 min

Text Processing: sort, uniq, cut, sed and awk

Slice, count, sort and transform text with cut, sort, uniq, tr, sed and awk, and combine them to analyze a real web server log.

Step 1 of 14Learn

Turning a log into an answer

A web server writes one line per request, something like this:

203.0.113.7 - - [14/Mar/2026:10:02:11] "GET /pricing HTTP/1.1" 200 5123
198.51.100.4 - - [14/Mar/2026:10:02:13] "GET /login HTTP/1.1" 404 312
203.0.113.7 - - [14/Mar/2026:10:02:15] "POST /login HTTP/1.1" 200 801

Each line has fields separated by spaces: the client IP is field 1, the status code is field 9, the size in bytes is field 10. A handful of tools let you slice and count these fields.

  • cut extracts columns. -d sets the delimiter and -f picks fields: cut -d' ' -f1 access.log prints just the IPs. For a CSV file: cut -d, -f2,4 data.csv prints columns 2 and 4.
  • sort sorts lines alphabetically. -n sorts numerically (so 10 comes after 9, not after 1), -r reverses, -k2 sorts by the second field, and -u removes duplicates.
  • uniq collapses adjacent duplicate lines into one. uniq -c puts a count in front of each. Because it only compares neighbors, you almost always sort first.
  • tr translates characters: tr 'a-z' 'A-Z' uppercases text, tr -d '\r' deletes Windows carriage returns that sometimes break scripts.

Step 2 of 14Practice · Diagram

Fields in one access-log line

Splitting on spaces numbers the pieces of each line. Tap a field.

Which field would you cut out to count error responses such as 404 and 500?

Tap the shape that answers the question, then press Check.

Fields in one access-log lineParts diagram: One log line, Field 1: 203.0.113.7, Field 4: [14/Mar/2026:10:02:11], Fields 6–7: "GET /pricing, Field 9: 200, Field 10: 5123.One log lineField 1:203.0.113.7Field 4:[14/Mar/20-26:10:02:11]Fields 6–7:"GET /pricingField 9: 200Field 10:5123
One log line
Field 1: 203.0.113.7

The client IP address.

Field 4: [14/Mar/2026:10:02:11]

When the request arrived.

Fields 6–7: "GET /pricing

The method and the page requested (field 7 is the path).

Field 9: 200

The HTTP status code: 200 OK, 404 not found, 500 server error.

Field 10: 5123

Bytes sent back.

Step 3 of 14Tap to reveal

Three more filters worth knowing

  1. paste and join

    paste a.txt b.txt glues two files side by side, line by line, separated by a tab. join merges two sorted files on a shared key field, a small command-line version of a spreadsheet lookup.

  2. column

    column -t lines up whitespace-separated text into neat columns. cat data.csv | column -t -s, turns a comma-separated file into a readable table right in the terminal.

  3. xargs

    xargs takes lines from standard input and turns them into arguments for another command. find . -name "*.tmp" | xargs rm removes every file found. It is handy when a command does not read standard input, such as rm. Names with spaces need find -print0 | xargs -0.

Step 4 of 14Practice · Choose one

A file contains the lines apple, banana, apple, cherry, banana, apple, in that order. What does uniq -c fruits.txt print (without sorting first)?

Step 5 of 14Learn

Reading the top-five pipeline

Now the pipeline from the last lesson makes sense, one stage at a time:

$ cut -d' ' -f1 access.log | sort | uniq -c | sort -rn | head -5
  1. cut -d' ' -f1 access.log prints the first field of every line: one IP address per request.
  2. sort groups identical IPs next to each other.
  3. uniq -c collapses each group into one line with a count, like 412 203.0.113.7.
  4. sort -rn sorts those lines numerically by the count, largest first.
  5. head -5 keeps the top five.

The same pattern, extract, sort, count, sort by count, take the top, answers dozens of questions: the most requested pages, the most common error messages, the users who log in most often. Change the field in step 1 and you have a new report.

Common mistakes with this pattern:

  • Forgetting the first sort, so uniq -c undercounts.
  • Using sort -r without -n at step 4. Text sorting puts "9" above "412" because it compares character by character.
  • Picking the wrong field number. Check with head -3 after the cut before building further.

Step 6 of 14Diagram

The top-five pipeline, stage by stage

Tap a stage to see what the data looks like after it.

The top-five pipeline, stage by stageProcess diagram: cut -d' ' -f1 → sort → uniq -c → sort -rn → head -5.cut -d' ' -f1sortuniq -csort -rnhead -5

Tap a shape with to see more.

cut -d' ' -f1

One IP per line, one line per request.

sort

Identical IPs now sit next to each other.

uniq -c

Each group becomes one line with a count: 412 203.0.113.7.

sort -rn

Numeric, reversed: biggest count first.

head -5

Keep the top five lines.

Step 7 of 14Practice · Put in order

Put the stages of a "five most requested pages" pipeline in the correct order. (In this log the page path is field 7.)

Drag the items, or use the arrow buttons, to put them in order.

  1. cut -d' ' -f7 access.log
  2. uniq -c
  3. sort -rn
  4. sort
  5. head -5

Step 8 of 14Learn

sed and awk: editing and reporting on streams

Two older but still essential tools handle jobs that cut and tr can't.

sed (stream editor) edits text as it flows past. Its most common use is search and replace:

$ sed 's/http:/https:/' links.txt        # first match on each line
$ sed 's/http:/https:/g' links.txt       # every match (g = global)
$ sed -i 's/Port 22/Port 2222/' sshd_config   # edit the file in place

The form is s/old/new/flags. Without -i, sed only prints the changed text and leaves the file alone, which is a safe way to preview. With -i, it rewrites the file, so make a backup first (sed -i.bak does that for you). sed -n '10,20p' file prints just lines 10 to 20.

awk treats each line as a record split into fields named $1, $2 and so on, with $NF meaning the last field. It is a small programming language, but one-liners cover most needs:

$ awk '{print $1, $9}' access.log              # IP and status code
$ awk '$9 == 500' access.log                   # only lines with status 500
$ awk '{sum += $10} END {print sum}' access.log  # total bytes served

Unlike cut, awk treats any run of spaces as a single separator, which makes it better for messy, space-aligned output such as ps or df. Use -F, to split on commas instead.

Step 9 of 14Practice · Match the pairs

Match each tool to the job it is best at.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. cut
  2. sort -n
  3. uniq -c
  4. tr
  5. sed
  6. awk

Step 10 of 14Compare

cut, sed and awk

Aspectcutsedawk
Best atPulling out columnsSearch and replaceField-based reports and filters
Splits onExactly one delimiter (-d)Works on whole linesAny run of spaces (or -F)
Examplecut -d, -f2 data.csvsed 's/http:/https:/g' links.txtawk '$9 == 500' access.log
Changes files?NoOnly with -iNo

Step 11 of 14Note

Step 12 of 14Practice · Fill in the blanks

Fill in the blanks

To replace every occurrence of colour with color on each line of essay.txt and print the result, run `sed 's/colour/color/' essay.txt. To print only the third field of each line in a space-separated file with awk, run awk '{print }' data.txt`.

Step 13 of 14Practice · Spot the mistake

This pipeline is supposed to show the 3 most common HTTP status codes (field 9) in access.log, most common first. Tap the stage that is wrong.

Step 14 of 14Practice · Choose one

df -h prints space-aligned columns, and you want only the "Use%" column, which is field 5. Which command handles the uneven spacing correctly?