Bash Sorting Text
Sorting text means putting the lines of a file into a set order. The sort command orders lines, and uniq removes repeated lines.
Numbers in the wrong order
Plain sort reads numbers as plain text. The line 10 lands before 9, and that order is wrong for counting.
echo "10" > nums.txt
echo "9" >> nums.txt
echo "2" >> nums.txt
sort nums.txtSorting words and reversing
sort puts words in A to Z order. Adding -r flips the result into Z to A order.
echo "banana" > fruit.txt
echo "apple" >> fruit.txt
echo "cherry" >> fruit.txt
sort fruit.txt
echo "---"
sort -r fruit.txtWhy uniq needs sorted input
uniq only removes repeats that sit side by side. Sorting first brings every repeat next to its twin.
echo "apple" > dup.txt
echo "banana" >> dup.txt
echo "apple" >> dup.txt
echo "Uniq on unsorted file:"
uniq dup.txt
echo "Sort first, then uniq:"
sort dup.txt | uniqExample
echo "10" > scores.txt
echo "9" >> scores.txt
echo "2" >> scores.txt
echo "10" >> scores.txt
echo "Numbers in order, no repeats:"
sort -n scores.txt | uniqKey points
- Use sort followed by a file name to print its lines in A to Z order.
- Plain sort treats numbers as text, so 10 can come before 9.
- Use sort -n to order numbers by their numeric value.
- Use sort -r to reverse the order of the sorted lines.
- uniq removes a repeated line only when it sits next to its twin.
- Pipe sorted output into uniq to remove every duplicate line.
In this example, the file holds four numbers with 10 written twice. The sort -n step orders them by value, so 2 and 9 come before 10. The pipe sends that order into uniq, and the second 10 disappears.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Which command orders the lines of nums.txt by numeric value?