A common idiom in command line processing of text files is
... | sort | uniq | ...
Some process produces lines of text. You want to pipe that text through sort
to sort the lines in alphabetical order, then pass it to uniq
to filter out all but the unique lines. The uniq
utility only removes adjacent duplicates, and so it will not remove all duplicates unless the input is sorted. (At least the duplicate lines need to be grouped together; the groups need not be in any particular order.)
When given the -u
flag, sort will sort and remove duplicates. This says the idiom above could be shortened to
... | sort -u | ...
Aside from saving a few keystrokes, is there any advantage to the latter? There could be, depending on how sort -u
is implemented. If internally it simply sorts its input and then removes duplicates, then there is no advantage. But if the code simultaneously sorts and removes duplicates, it could save memory and time, depending on the input. If the code discarded duplicates as their were encountered, the code would need working memory proportional to the amount of unique input rather than the total amount of input.
I had a project recently that makes a good test case for this. The Gutenberg text corpus contains a list of words for 55,000 different sources, each in a separate text file. There are a lot of files, and there is a lot of redundancy between files. The combined file is 3.4 GB.
Running sort -u
on the file took 394 seconds.
Running sort | uniq
on the file took 610 seconds.
So in this example, sort -u
not only saved a few keystrokes, it took about 35% off the time.
The post Sort and remove duplicates first appeared on John D. Cook.