Data comes at you messy. Logs are out of order, lists have duplicates, and someone always enters names in random order. sort and uniq are the two commands that clean things up. You will almost always use them together.
Sorting lines with sort
The sort command takes lines of text and arranges them alphabetically by default. It does not change the original file โ it just prints the sorted version to the terminal.
$ cat fruits.txt
banana
apple
date
cherry
$ sort fruits.txt
apple
banana
cherry
date
Try it Yourself โ
Numeric sort with -n
Sorting numbers alphabetically gives you weird results โ 10 comes before 2 because "1" is smaller than "2". The -n flag sorts by numeric value instead.
$ cat scores.txt
85
92
100
78
$ sort -n scores.txt
78
85
92
100
Try it Yourself โ
Reverse sort with -r
Add -r to reverse the order. Highest scores first, latest dates first, Z before A โ whatever you need flipped.
$ sort -rn scores.txt
100
92
85
78
Try it Yourself โ
Unique lines with uniq
uniq removes consecutive duplicate lines. That means you need to sort first so duplicates are next to each other, then pipe into uniq. Together they are unstoppable.
$ cat names.txt
Alice
Bob
Alice
Charlie
Bob
$ sort names.txt | uniq
Alice
Bob
Charlie
Add -c to count how many times each line appears โ super useful for finding the most common entries in a log file.
$ sort names.txt | uniq -c
2 Alice
2 Bob
1 Charlie
Try it Yourself โ