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.