Sorting and counting

  • sort puts lines in order: -r reverses it, and -n sorts by number instead of by text.
  • uniq collapses adjacent duplicate lines, and -c counts them.

That word adjacent matters. uniq only compares each line with the one right before it, so you nearly always sort first:

uniq names.txt
sort names.txt | uniq
sort names.txt | uniq -c

Real world: sort | uniq -c is the shell's GROUP BY. It's how you count visitors per IP, errors per type, or commits per author.