Sort -u vs. uniq

I have sometimes seen people use a pipeline that includes "sort | uniq". The result of that is no different than just adding a -u flag to sort and absolutely requires more time and processing power - not that it usually matters; unless the input is humongously long, you'd need to run them through "time" to spot any difference. So why use "uniq"?

For cases like that, where there is no difference in the output, it's probably just habit - you may be accustomed to using "uniq" for other jobs and just reach for it automatically. I'll argue that it's a good habit to have: if you are in the habit of using "sort -u", you may tend to forget about "uniq" and that could cause you do do something much more difficult and clumsy when a job needs something that "uniq" does well.

However, it's also true that "sort" has tricks that "uniq" lacks, so if you only know about "uniq", you again could make your life more difficult.

One of the helpful abilities that "sort" has is the ability to specify the field separator. Let's take a sample file:


A:B:C:D
a:b:c:d
t:b:c:d
t:b:c:d
a:b:c:d
a:b:x:d
foo:b:x:d
f:a:x:d
 

If all we cared about was removing duplicatelines, we could use "sort -u file" or "sort file | uniq". But what if we want to sort by the second field?

We can do that directly with "sort -t: -k 2 -u", but it's much harder to do with "uniq" because you can't tell it a separator character. You can get around that partially with "tr" or "sed", translating ":"'s to spaces or tabs, but that's clumsy. Even after translating, "uniq" only lets you skip fields, so you don't get quite the same output:

$ sort -t: -k 2 -u file
A:B:C:D
f:a:x:d
a:b:c:d
a:b:x:d
$ cat file | tr ":" " " | sort | uniq -f1
A B C D
a b c d
a b x d
f a x d
foo b x d
t b c d
 

We could argue about which output truly represents unique lines when sorted on field 2, but the point to understand is that skipping fields isn't the same as what "sort" does.

You can also lock down fields with "sort" :

$ sort -t: -u -k2,2   file
A:B:C:D
f:a:x:d
a:b:c:d
 

As "uniq" can only skip fields and can't anchor to one field only, it's much harder to get these results. However, "uniq" again has tricks that "sort" can't do: it can skip a specific number of characters in addition to skipping fields. It can also give you only the unique lines or only the lines that were repeated:

$ sort file | uniq -u  # only the unique, non-repeated lines
A:B:C:D
a:b:x:d
f:a:x:d
foo:b:x:d
$ sort file | uniq -d  # repeated lines
a:b:c:d
t:b:c:d
 

Either of those is extremely convoluted without "uniq", and the need for one or the other does come up surprisingly often.

Somebody thought that we could use "sort" and "uniq" in one program: Sortu is the result.


The sortu program is a replacement for the sort and uniq programs. It is common for Unix script writers to want to count how many separate patterns are in a file. For example, if you have a list of addresses, you may want to see how many are from each state. So you cut out the state part, sort these, and then pass them through uniq -c. Sortu does all this for you in a fraction of the time.

I think by the time I figured out how to use "sortu" I could have already done the job another way, but you might find it interesting anyway.

I think the important thing is to realize that "sort" and "uniq" have both conflicting and complementary abilities. Don't tie yourself in pipeline knots with either of them; learn to use each of them appropriately and your scripts will be easier.



Got something to add? Send me email.



5 comments



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Anthony Lawrence







Mon Nov 16 01:38:48 2009: 7554   ScottCarpenter

gravatar
I learn and forget about uniq occasionally. sort -u usually does the job for me, but the other day I wanted to count how many occurrences there were of each thing, which led me to rediscovering uniq -c in concert with sort.





Mon Nov 16 04:35:21 2009: 7555   anonymous

gravatar
Are there any versions of sort out there that do not have the -u option? I'm wondering if the reason some people use sort | uniq is portability...





Mon Nov 16 11:11:56 2009: 7556   TonyLawrence

gravatar
Are there any versions of sort out there that do not have the -u option?

I son't think so - it's had that ever since I can remember.



Mon Nov 16 13:41:53 2009: 7559   BigDumbDinosaur

gravatar
sortu is the sort (ouch!) of creeping featurism that seems to be taking over in present day Linux distributions. We should keep that sort of programming in the Windows camp and stick to the UNIX philosophy of making small tools that do a small group of related tasks well.

As for uniq, I know about it but for some reason have never found a good reason to use it. I guess sort and pipes are too entrenched in my mind.



Mon Nov 16 20:04:38 2009: 7560   TonyLawrence

gravatar
But "uniq" can do things sort -u cannot...

------------------------
Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us