Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I use GNU sort and uniq to have the most common occurrences on top instead of numerical or alphanumerical sorting? Example list.txt:


Since '2' occurs 3 times, should be on top, followed by '3' and '1' like this:

$ cat list.txt | "some sort/uniq magic combo"
share|improve this question
up vote 4 down vote accepted

Like this:

cat list.txt | sort | uniq -c | sort -rn

The -c includes the count of each unique line and then you sort by that.

If you want to remove the count after sorting, do so:

cat list.txt | sort | uniq -c | sort -rn | awk '{ print $2; }'
share|improve this answer
I've been doing this for ages, and for moderate size tasks it works well. However every so often I find myself with gigabytes of log data to go through and doing a sort on that requires a lot of disk space that is for duplicate lines that you throw away in the next step. There are better algorithms, but I don't know good simple command line tools for solving this problem at a larger scale. – mc0e Feb 18 '15 at 15:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.