Linux on call · Chapter 9 of 30
Sorting and counting
By the end of this chapter you can cut a column out of a CSV file, sort it as text or as numbers, and count how often each value appears. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Files and text
- 4 tasks
- Linux
- In the all labs plan
The first 2 chapters of Linux on call are free. This one comes with the all labs plan.
Why you need this
Ravi has today's export from the ordering app, orders.csv: one line per order, 37 orders. He wants three things before lunch: the list of items sold today, the five most ordered items, and the three biggest orders.
Bunty tried uniq -c on the item column, and his top item was Vada pav, ordered once. grep can find the lines for one item, but it cannot tell you which items there are, count every one of them, or rank orders by their size.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Save every item sold today, once each, in items.txt
Make a list of every item sold today, with each item only once.
1. Run head -3 orders.csv. Line 1 is a header. The item is the third field.
2. Skip the header line, and keep only the item field. Use cut.
3. Sort the items, and keep one of each. Use sort.
4. Save the result in items.txt, in the chapter folder.
To pass: items.txt has every item from orders.csv, one per line, each only once, in alphabetical order, and nothing else. No header line (item), no counts, no other fields.
- Task 2
Save the five most ordered items, with counts, in top5.txt
Find the five items that people ordered most often today.
1. Start like task 1: skip the header and keep only the item field.
2. Sort the items, then count each one with uniq -c.
3. Sort the counts as numbers, biggest first.
4. Keep only the first five lines, and save them in top5.txt.
To pass: top5.txt has exactly five lines. Each line has an item and its number of orders, most first, and nothing else. Count orders (lines), not the qty field.
- Task 3
Save the three biggest orders in biggest.txt
Find the three orders with the biggest quantity.
1. Skip the header line of orders.csv. Keep whole lines this time.
2. Sort the lines by the fourth field (qty), as numbers, biggest first. Use sort with -t and -k.
3. Keep only the first three lines, and save them in biggest.txt.
To pass: biggest.txt has the three whole order lines with the biggest qty, biggest first, exactly as they are in orders.csv (all four fields), and nothing else.
- Task 4 · interview question
Interview: why uniq needs sort first, in uniq.txt
Interview question: why does uniq need sort first?
1. Run the count from task 2 without sort before uniq -c. Compare it with the sorted count.
2. Write one sentence in your own words. Say which repeated lines uniq can join into one.
3. Say what sort does that makes the count right.
4. Save the sentence in uniq.txt, in the chapter folder.
To pass: uniq.txt has your sentence, and it says where two repeated lines must be for uniq to join them.
What you will learn
- cut: keep one field of each line, split at a character you choose.
- sort: put lines in order, as text or as numbers (-n), reversed (-r), one of each (-u), by a field (-t and -k).
- uniq -c: print a repeated line once, with its count.
- head and tail on a pipe: keep the top lines, or skip a header.
More in Files and text
Build "Sorting and counting" yourself
Tapri’s ordering app moves onto a Linux server, and the server is now yours to run. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Linux on call are free. This one comes with the all labs plan.