Unix shell tricks - sort and uniq
By yuli - Posted on January 31st, 2008
I have read a great article about sorting command tricks and would like to share one of the tricks with you.
Let's say you have non-unique, textual file from which you need to remove duplicate strings preserving file order. One will suggest to do it in Perl, though it could be done using basic shell commands:
cat file.txt | nl | sort -k2 -u | sort -n | cut -f2 > result.txt
Run the above shell command as follows:
- Read the file.txt file.
- Line numbers are added to each line of the original file.
- Sort and remove duplicate strings using the second field as if it were the original file.
- Resort the results according to the line number.
- Remove the line numbers from the results.
- Save the results.
Note
The nl command adds line numbers to the text file.











