2

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:

1
2
2
2
3
3

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

$ cat list.txt | "some sort/uniq magic combo"
2
3
1
719016
  • 4,683

1 Answers1

5

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; }'
Doug Harris
  • 28,397