Skip to content

Count and rank: sort, uniq and wc

Lesson 14 of 167 min

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

So sort first, which puts identical lines side by side, then uniq:

sort votes.txt | uniq

Counting

uniq -c puts a count in front of each line:

sort votes.txt | uniq -c

Ranking

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

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

Next →