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.
More Articles by Anthony Lawrence - Find me on Google+
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. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
Mon Nov 16 01:38:48 2009: ScottCarpenter
http://www.movingtofreedom.org
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: anonymous
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: TonyLawrence
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: BigDumbDinosaur
http://bcstechnology.net
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: TonyLawrence
But "uniq" can do things sort -u cannot...
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar