TExcelTAD
Home › Linux & Bash › sort & uniq

sort & uniq

sort puts lines in order; uniq collapses adjacent duplicate lines. Together they answer questions like "which IP addresses hit my server most?" or "what are the distinct values in this column?", and sort | uniq -c | sort -rn is one of the most famous pipelines in Unix.

Sorting lines

bash · try it yourself
printf 'pear\nApple\nbanana\napple\ncherry\n' > fruit.txt
sort fruit.txt
echo "--- reverse:"
sort -r fruit.txt
echo "--- ignore case:"
sort -f fruit.txt
Apple
apple
banana
cherry
pear
--- reverse:
pear
cherry
banana
apple
Apple
--- ignore case:
Apple
apple
banana
cherry
pear

In this sandbox sort compares characters by their codes (the C locale), so capital letters come first. Many desktop systems use a language-aware locale that sorts apple and Apple together; set LC_ALL=C when a script needs the same order everywhere.

Numbers need -n

By default sort compares text character by character, so 10 comes before 9. Use -n for numbers, -h for human sizes (2K, 1G) and -V for version numbers:

bash · try it yourself
printf '10\n9\n100\n25\n' > numbers.txt
echo "text:    $(sort numbers.txt | tr '\n' ' ')"
echo "numeric: $(sort -n numbers.txt | tr '\n' ' ')"
printf '1.5G\n200M\n3K\n' | sort -h | tr '\n' ' '; echo
printf 'v1.10\nv1.9\nv1.2\n' | sort -V | tr '\n' ' '; echo
text:    10 100 25 9
numeric: 9 10 25 100
3K 200M 1.5G
v1.2 v1.9 v1.10

Sorting by a column

-k picks the key field and -t the separator (default: whitespace). -k2,2 means "field 2 only"; -k2 alone would mean "from field 2 to the end of the line":

bash · try it yourself
cat > scores.csv <<'CSV'
ada,engineering,91
linus,kernel,78
grace,navy,95
margaret,apollo,78
CSV
sort -t, -k3,3n scores.csv
echo "--- highest first:"
sort -t, -k3,3nr scores.csv | head -n 2
echo "--- by score, then name:"
sort -t, -k3,3n -k1,1 scores.csv
linus,kernel,78
margaret,apollo,78
ada,engineering,91
grace,navy,95
--- highest first:
grace,navy,95
ada,engineering,91
--- by score, then name:
linus,kernel,78
margaret,apollo,78
ada,engineering,91
grace,navy,95

The n and r letters after the key apply only to that key, so you can sort numerically descending on one column and alphabetically on another.

Other sort options

Option Effect
-u Output each distinct line once (like sort | uniq)
-o FILE Write the result to a file (safe even if it's the input file)
-c Only check whether input is sorted
-s Stable: keep the original order of equal lines
-R Shuffle (random order)

Warning: sort data.txt > data.txt empties the file before sort reads it. Use sort -o data.txt data.txt instead.

uniq: removing adjacent duplicates

uniq only compares each line with the one before it, which is why it almost always comes after sort:

bash · try it yourself
printf 'red\nred\nblue\nred\nblue\nblue\n' > colors.txt
echo "uniq alone: $(uniq colors.txt | tr '\n' ' ')"
echo "sort|uniq:  $(sort colors.txt | uniq | tr '\n' ' ')"
echo "--- counts:"
sort colors.txt | uniq -c
uniq alone: red blue red blue
sort|uniq:  blue red
--- counts:
      3 blue
      3 red
Option Effect
-c Prefix each line with how many times it occurred
-d Only print lines that are duplicated
-u Only print lines that are not duplicated
-i Ignore case
-f N Skip the first N fields when comparing

Top-N reports

The pipeline sort | uniq -c | sort -rn | head gives a ranked list of the most frequent values:

bash · try it yourself
cat > access.log <<'LOG'
10.0.0.7 GET /index.html
10.0.0.3 GET /about.html
10.0.0.7 GET /pricing.html
10.0.0.9 POST /login
10.0.0.7 GET /index.html
10.0.0.3 GET /index.html
10.0.0.7 POST /login
LOG
echo "Top visitors:"
cut -d' ' -f1 access.log | sort | uniq -c | sort -rn | head -n 2
echo "Pages requested once:"
cut -d' ' -f3 access.log | sort | uniq -u
echo "Distinct pages: $(cut -d' ' -f3 access.log | sort -u | wc -l)"
Top visitors:
      4 10.0.0.7
      2 10.0.0.3
Pages requested once:
/about.html
/pricing.html
Distinct pages: 4

Step by step: cut extracts the IP, sort groups equal IPs together, uniq -c counts each group, sort -rn ranks by count (numerically, largest first) and head keeps the top.

Comparing sorted lists with comm

comm compares two sorted files and prints three columns: lines only in the first, only in the second, and in both. -1, -2, -3 hide columns:

bash · try it yourself
printf 'ada\ngrace\nlinus\n' > team-2025.txt
printf 'ada\nlinus\nmargaret\n' > team-2026.txt
echo "left:   $(comm -23 team-2025.txt team-2026.txt)"
echo "joined: $(comm -13 team-2025.txt team-2026.txt)"
echo "stayed: $(comm -12 team-2025.txt team-2026.txt | tr '\n' ' ')"
left:   grace
joined: margaret
stayed: ada linus

Test yourself

Why is uniq usually used after sort?

  1. uniq only works on numbers
  2. uniq only removes duplicates that are on adjacent lines
  3. sort removes duplicates first
  4. uniq needs a sorted header

Which command sorts numerically by the third comma-separated field?

  1. sort -k3 file
  2. sort -t, -k3,3n file
  3. sort -n3 file
  4. sort -t3 -n file

What does sort | uniq -c | sort -rn produce?

  1. Lines in reverse alphabetical order
  2. Distinct lines with their counts, most frequent first
  3. Only duplicated lines
  4. The number of lines