Sort Unique column in Unix

I have been looking for an answer for a good amount of time but it seems that it hasn't been answered yet. I do apologize if this question has already been answered.

So i am trying to sort the number of appearances from each ID from High to Low.

From the following command: sort -k1,1 File.txt | uniq -c I get this result :

1 21423 
3 21447
5 21448
3 21513
1 21584 
1 21585
1 21586

The result I want:

5 21448
3 21447
3 21513
1 21423 
1 21584 
1 21585
1 21586

It does sort the second column even though I typed -k1,1 I have tried different parameters and options such as -n. Any leads would help me a lot. Thanks in advance!

1 answer

  • answered 2017-11-12 20:29 janos

    You need to sort the output of uniq -c:

    ... | uniq -c | sort -nr
    

    When you want to sort lines by the count, it doesn't matter how the items were sorted before calling uniq -c, because the counts will only be known after the call to uniq -c.