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.
Welcome back!
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 801Each 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.
cutextracts columns.-dsets the delimiter and-fpicks fields:cut -d' ' -f1 access.logprints just the IPs. For a CSV file:cut -d, -f2,4 data.csvprints columns 2 and 4.sortsorts lines alphabetically.-nsorts numerically (so 10 comes after 9, not after 1),-rreverses,-k2sorts by the second field, and-uremoves duplicates.uniqcollapses adjacent duplicate lines into one.uniq -cputs a count in front of each. Because it only compares neighbors, you almost alwayssortfirst.trtranslates 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.
- 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
paste and join
paste a.txt b.txtglues two files side by side, line by line, separated by a tab.joinmerges two sorted files on a shared key field, a small command-line version of a spreadsheet lookup.column
column -tlines 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.xargs
xargstakes lines from standard input and turns them into arguments for another command.find . -name "*.tmp" | xargs rmremoves every file found. It is handy when a command does not read standard input, such asrm. Names with spaces needfind -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 -5cut -d' ' -f1 access.logprints the first field of every line: one IP address per request.sortgroups identical IPs next to each other.uniq -ccollapses each group into one line with a count, like412 203.0.113.7.sort -rnsorts those lines numerically by the count, largest first.head -5keeps 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, souniq -cundercounts. - Using
sort -rwithout-nat step 4. Text sorting puts "9" above "412" because it compares character by character. - Picking the wrong field number. Check with
head -3after thecutbefore 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.
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.
- cut -d' ' -f7 access.log
- uniq -c
- sort -rn
- sort
- 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 placeThe 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 servedUnlike 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).
- cut
- sort -n
- uniq -c
- tr
- sed
- awk
Step 10 of 14Compare
cut, sed and awk
| Aspect | cut | sed | awk |
|---|---|---|---|
| Best at | Pulling out columns | Search and replace | Field-based reports and filters |
| Splits on | Exactly one delimiter (-d) | Works on whole lines | Any run of spaces (or -F) |
| Example | cut -d, -f2 data.csv | sed 's/http:/https:/g' links.txt | awk '$9 == 500' access.log |
| Changes files? | No | Only with -i | No |
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?
Lesson complete!