terminal.cab

Linux Basics Lesson 10 of 38

guestLevel 1Badges and progress0/100 XP, XP to the next levelStreak0

Sort and count

10 minutes

Learning goals

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:

  • -n as numbers, otherwise 10 comes before 9
  • -h sizes like 95M and 2.1G
  • -o FILE write the result to FILE, which may be the input

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

in the glossary: sort

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

  • -d, the separator, here a comma
  • -f1,3 columns 1 and 3

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,95

in the glossary: cut

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

  • -c count each group

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

in the glossary: sort

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

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

Predict the output

Command

uniq visitors.txt | wc -l

Before the command, in your home folder ~

  • ~/visitors.txt
What does uniq visitors.txt | wc -l print?

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

  1. Question 1 of 3Why does sort come before uniq -c?

    Check works once you have chosen an answer.

  2. Question 2 of 3Which option puts 2.1G behind 95M?

    Check works once you have chosen an answer.

  3. Question 3 of 3What does cut -d, -f1,3 data.csv print?

    Check works once you have chosen an answer.

Cards

  1. Card 1 of 3

    Tap the card or press Space to turn it over.

  2. Card 2 of 3

    Tap the card or press Space to turn it over.

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

Next: lesson 11, Changing text with sed (9 min)

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

  1. Lesson 1: open
  2. Lesson 2: open
  3. Lesson 3: open
  4. Lesson 4: open
  5. Lesson 5: open
  6. Lesson 6: open
  7. Lesson 7: open
  8. Lesson 8: open
  9. Lesson 9: open
  10. Lesson 10: done
  11. Lesson 11: open
  12. Lesson 12: open
  13. Lesson 13: open
  14. Lesson 14: open
  15. Lesson 15: open
  16. Lesson 16: open
  17. Lesson 17: open
  18. Lesson 18: open
  19. Lesson 19: open
  20. Lesson 20: open
  21. Lesson 21: open
  22. Lesson 22: open
  23. Lesson 23: open
  24. Lesson 24: open
  25. Lesson 25: open
  26. Lesson 26: open
  27. Lesson 27: open
  28. Lesson 28: open
  29. Lesson 29: open
  30. Lesson 30: open
  31. Lesson 31: open
  32. Lesson 32: open
  33. Lesson 33: open
  34. Lesson 34: open
  35. Lesson 35: open
  36. Lesson 36: open
  37. Lesson 37: open
  38. Lesson 38: open
1 of 38

Next: lesson 11, Changing text with sed (9 min) That's enough for today

Path complete

You finished all 38 lessons of Linux Basics.

Back to the path

Cheat sheet

The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.