Linux Basics Lesson 10 of 38
Sort and count
10 minutes
Learning goals
- Sort by number, by size and by a column with sort -n, -h, -r, -k and -t.
- Cut a column out of a file with cut -d and -f.
- Build a statistic with the chain sort | uniq -c | sort -rn and explain why sort comes first.
Sorting: sort
sort sorts lines as text, and that means 300 comes before 45, because 3 comes before 4. -n sorts them as numbers. -t names the
separator and -k the column. -r turns the order around:
sort -t, -k3 -n orders.csv
Purpose: Sorts lines. Without options as text, with -n as numbers, with -k by a column.
Syntax: sort [-nrh] [-t SEP] [-k COLUMN] [-o OUT] [FILE]
Options:
Example:
sort -t, -k3 -n orders.csv
customer,city,amount
finn,Hamburg,15
cem,Berlin,45
eva,Berlin,60
ben,Hamburg,80
hugo,Cologne,95
anna,Berlin,120
dana,Munich,200
gina,Berlin,300
sort -t, -k3 -rn orders.csv | head -n 2
gina,Berlin,300
dana,Munich,200-h understands sizes like 95M and 2.1G, which -n would misread. The list sizes.txt is the kind of thing du -sh * produces:
sort -h sizes.txt
512 .bashrc
4.0K notes.txt
12K logs
44K projects
95M archive
2.1G backups
sort -rh sizes.txt | head -n 2
2.1G backups
95M archive
On a real server you get the biggest folders with du -sh * | sort -rh | head, the bridge to du from module 2. This lesson does not
load du, so you sort the list instead.
Cutting out a column: cut
cut takes one column from each line. -d is the separator, -f the column. For files with a clean separator, such as CSV or
/etc/passwd, it is the fastest tool:
cut -d, -f2 orders.csv
Purpose: Cuts columns out of each line, split at a fixed separator.
Syntax: cut -d SEP -f COLUMNS [FILE]
Options:
Example:
cut -d, -f2 orders.csv
city
Berlin
Hamburg
Berlin
Munich
Berlin
Hamburg
Berlin
Cologne
cut -d, -f1,3 orders.csv
customer,amount
anna,120
ben,80
cem,45
dana,200
eva,60
finn,15
gina,300
hugo,95Counting: uniq
uniq folds identical lines that stand directly below each other, and -c puts the number in front. Without sort, that goes
quietly wrong:
sort visitors.txt | uniq -c
Purpose: Folds identical neighbouring lines into one. -c puts the count in front.
Syntax: uniq [-c] [FILE]
Options:
Example:
uniq -c visitors.txt
1 10.0.0.5
1 10.0.0.9
2 10.0.0.5
1 10.0.0.7
1 10.0.0.9
1 10.0.0.5
1 10.0.0.9
sort visitors.txt | uniq -c
4 10.0.0.5
1 10.0.0.7
3 10.0.0.9The first answer has 10.0.0.5 three times, with different numbers, and no error message says so. sort first puts equal lines next
to each other, and then the count is right.
The chain
Now the whole thing: cut out the column, sort so that equal values stand together, count, sort by the count with the biggest on top,
keep the top. Skip the header line with tail -n +2:
cut -d, -f2 orders.csv | tail -n +2 | sort | uniq -c | sort -rn
4 Berlin
2 Hamburg
1 Munich
1 Cologne
cut -d, -f2 orders.csv | tail -n +2 | sort | uniq -c | sort -rn | head -n 1
4 Berlin
Practice
Terminal task 1/3
Who visits most often? Count the lines of visitors.txt per address, with the biggest count on top.
Task 1/3: Who visits most often? Count the lines of visitors.txt per address, with the biggest count on top.
Who visits most often? Count the lines of visitors.txt per address, with the biggest count on top.
Task complete
Solution
sort visitors.txt | uniq -c | sort -rn Why: The first sort groups equal lines, uniq -c counts each group, and the second sort orders by that count. -n because the counts are numbers, -r to get the biggest first.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 2/3
sizes.txt lists folders with their sizes. Print only the line with the biggest size.
Task 2/3: sizes.txt lists folders with their sizes. Print only the line with the biggest size.
sizes.txt lists folders with their sizes. Print only the line with the biggest size.
Task complete
Solution
sort -rh sizes.txt | head -n 1 Why: With -n, 95M would beat 2.1G, because 95 is bigger than 2. -h reads the unit too. It is the same pair as du -sh * | sort -rh on a real server.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 3/3
Save orders.csv sorted by amount (third column, as a number) in the file sorted.csv.
Task 3/3: Save orders.csv sorted by amount (third column, as a number) in the file sorted.csv.
Save orders.csv sorted by amount (third column, as a number) in the file sorted.csv.
Task complete
Solution
sort -t, -k3 -n -o sorted.csv orders.csv Why: Here, -o and > come to the same thing. They only differ when the output is also the input, and that is the one case where -o saves you.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Predict the output
Command
uniq visitors.txt | wc -lBefore the command, in your home folder ~
- ~/visitors.txt
Check works once you have chosen an answer.
Fill the gap
Print the second column of a file with commas: cut -d, orders.csv
Print the second column of a file with commas: cut -d, … orders.csv
Check works once you have typed an answer.
Next up: changing text without opening an editor.
Quiz
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
Cards
-
Card 1 of 3
Tap the card or press Space to turn it over.
-
Card 2 of 3
Tap the card or press Space to turn it over.
-
Card 3 of 3
Tap the card or press Space to turn it over.
End of the lesson
Still open: 5 exercises, 3 quiz questions
What you can do now: sort -t, -k3 -n orders.csv, sort visitors.txt | uniq -c, cut -d, -f2 orders.csv
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: open
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- Lesson 10: done
- Lesson 11: open
- Lesson 12: open
- Lesson 13: open
- Lesson 14: open
- Lesson 15: open
- Lesson 16: open
- Lesson 17: open
- Lesson 18: open
- Lesson 19: open
- Lesson 20: open
- Lesson 21: open
- Lesson 22: open
- Lesson 23: open
- Lesson 24: open
- Lesson 25: open
- Lesson 26: open
- Lesson 27: open
- Lesson 28: open
- Lesson 29: open
- Lesson 30: open
- Lesson 31: open
- Lesson 32: open
- Lesson 33: open
- Lesson 34: open
- Lesson 35: open
- Lesson 36: open
- Lesson 37: open
- Lesson 38: open
Cheat sheet
-
sort -t, -k3 -n <file>by the third column, as a number -
sort -rhsizes, biggest first -
sort -o <file> <file>sort a file in place -
cut -d, -f2 <file>one column of a CSV -
sort | uniq -c | sort -rnthe statistic chain
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 20 KB)
- View as image (PNG, 73 KB)