🎓 A1Learner
🔍
0 of 46 lessons completed

Bash Sort and Uniq in Depth

Sort and uniq are commands that order lines and count repeated lines. Sort arranges lines, and uniq reports or removes lines that repeat next to each other.

Sort on one column as a number

The -t flag names the colon separator, and -k picks the second field. The -n flag compares the ages as numbers, not as text.

printf 'bob:25\nana:30\ncid:20\n' | sort -t: -k2,2n

Numbers sort by value, not by first digit

Plain sort would put 10 before 2, because 1 comes before 2. The -n flag puts 2 first, as numbers should be.

printf '10\n2\n30\n4\n' | sort -n

Count each word with uniq -c

Sorting first groups equal words together. Then uniq -c prints each word once with its count, in byte order under LC_ALL=C.

printf 'apple\nbanana\napple\ncherry\nbanana\napple\n' | LC_ALL=C sort | uniq -c

Example

printf 'apple\nbanana\napple\ncherry\nbanana\napple\n' | LC_ALL=C sort | uniq -c | sort -nr

Key points

  • sort -t : -k 2,2n: sort on the second colon-separated field as a number.
  • sort -n: sort by number value, so 2 comes before 10.
  • sort -r: reverse the sort order, so the largest value comes first.
  • uniq -c: print each repeated line once, with its count in front.
  • uniq only sees repeats that sit next to each other, so sort first.
  • LC_ALL=C: a setting that makes sort use plain byte order.

In this example, LC_ALL=C sort groups equal words together in byte order. Then uniq -c prints each word once with its count. Finally sort -nr orders those count lines by number, largest first, so the most common word tops the list.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Why must uniq -c input be sorted first?