APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Sort -u vs. uniq

© November 2009 Anthony Lawrence

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:


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
$ cat file | tr ":" " " | sort | uniq -f1
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

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
$ sort file | uniq -d  # repeated lines

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.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Sort -u vs. uniq


Inexpensive and informative Apple related e-books:

Take Control of Preview

Sierra: A Take Control Crash Course

Are Your Bits Flipped?

Take Control of IOS 11

Take Control of iCloud

More Articles by © Anthony Lawrence

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

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

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

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

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

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


Printer Friendly Version

Have you tried Searching this site?

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

Printer Friendly Version

Java is the most distressing thing to hit computing since MS-DOS. (Alan Kay)

Linux posts

Troubleshooting posts

This post tagged:





Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode

SCO Unix Sales, Support, & Service

Phone:  707-SCO-UNIX (707-726-8649Toll Free: 833-SCO-UNIX (833-726-8649)