Count and rank: sort, uniq and wc
Combine sort, uniq -c and sort -nr into the classic pipeline that counts and ranks repeated lines, like votes or log entries. Practise it in your browser.
votes.txt has one line per vote for a favourite fruit. Which fruit won? Three filters answer it.
uniq only sees neighbours
uniq collapses repeated lines into one, but only when the repeats are next to each other. Run it on the raw votes and you'll see mango several times:
uniq votes.txtSo sort first, which puts identical lines side by side, then uniq:
sort votes.txt | uniqCounting
uniq -c puts a count in front of each line:
sort votes.txt | uniq -cRanking
The counts are now at the start of each line, so a numeric sort ranks them. -n sorts by number (so 10 comes after 9, not after 1) and -r puts the biggest first:
sort votes.txt | uniq -c | sort -nrAdd | head -n 3 for a top three. This one pipeline, sort | uniq -c | sort -nr, answers lots of everyday questions: the most common error in a log, the busiest visitor, the most used word.
wc -l completes the set: wc -l votes.txt is the number of votes, and sort -u votes.txt | wc -l the number of different fruits.
Your turn
Show each fruit in votes.txt with its number of votes, most votes first.
Your task
Show each fruit in votes.txt with its number of votes, most votes first.
- Every fruit with its count, highest first (not done yet)
The checklist ticks itself off as you work in the terminal: any way that gets the result counts.
Hints come one at a time, then one way to do it. Try each before the next.
What this lesson uses, in one place.
sort- Sort lines of text More on sort →
uniq- Remove or count repeated lines More on uniq →
uniq -c- Put the number of times each line occurred in front of it More on uniq →
sort -nr- Sort by the number at the start of each line, so 9 comes before 10; reverse the order More on sort →