sort, uniq, cut and tr

grep, sed & awk · Practical · 12 min

grep, sed and awk find and reshape data; these helpers rank, count and tidy it into an answer.

A few small coreutils appear in almost every text pipeline:

  • sort orders lines. -n numeric, -r reverse, -h human sizes (2K, 1G), -u unique, -t ',' separator, -k2,2 sort by field 2 only. Always write the end of the key (-k2,2): -k2 means from field 2 to the end of the line.
  • uniq collapses adjacent duplicate lines, so it is nearly always preceded by sort. uniq -c prefixes counts, -d shows only duplicated lines.
  • cut extracts columns by delimiter (cut -d, -f1,3) or by character position (cut -c1-8). Unlike awk it treats every delimiter as a separator, so two spaces mean an empty field between them.
  • tr translates or deletes characters: tr 'a-z' 'A-Z', tr -d '\r' strips Windows carriage returns, tr -s ' ' squeezes repeated spaces.
  • head -n, tail -n, wc -l take the top, the bottom and the count.

The "top N" idiom combines them:

... | sort | uniq -c | sort -rn | head

It counts each distinct line, then ranks by count.

sort is locale-aware. With LC_ALL=C sort you get plain byte order, which is faster and identical on every machine, important when another tool (like join or comm) expects sorted input.

Example

printf '10.0.0.1 GET\n10.0.0.2 GET\n10.0.0.1 POST\n10.0.0.1 GET\n' > hits.txt

# top client IPs
cut -d' ' -f1 hits.txt | sort | uniq -c | sort -rn
#       3 10.0.0.1
#       1 10.0.0.2

printf 'b,2\na,10\nc,1\n' | sort -t, -k2,2n     # c,1 / b,2 / a,10
printf 'b,2\na,10\nc,1\n' | sort -t, -k2,2      # c,1 / a,10 / b,2  (text order!)

echo 'Hello,   World' | tr -s ' ' | tr 'a-z' 'A-Z'   # HELLO, WORLD
printf 'line\r\n' | tr -d '\r' | od -c | head -1   # no \r left

Remember

  • sort | uniq -c | sort -rn | head is the top-N idiom
  • uniq only merges adjacent lines, so sort first
  • Use -k2,2 with -n for numeric sorting on one field

Watch out for

  • Sorting numbers without -n, so 10 comes before 2.

Check yourself

  1. Why is sort usually placed before uniq?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: www.gnu.org/software/coreutils/manual/coreutils.html#sort-invocation