Sort by number of occurrences

14,425
some_command | sort | uniq -c | sort -n
Share:
14,425

Related videos on Youtube

MultiplyByZer0
Author by

MultiplyByZer0

I am a strong believer in public domain and the abolition of copyright. All text that I write is licensed under CC0. All code that I create is licensed under the Unlicense. These licenses grant you total freedom from copyright, and you will never be obligated to do anything for using content I created. Email: [email protected]

Updated on November 01, 2020

Comments

  • MultiplyByZer0
    MultiplyByZer0 over 3 years

    What's the quickest way to sort items by number of occurrences on a Linux terminal?

    Ideally, I'm looking for a one-liner.

  • Nate Glenn
    Nate Glenn over 8 years
    You'll probably want sort -nr to put the most common lines first.
  • cdhowie
    cdhowie over 8 years
    @NateGlenn Depends. Usually there are pages of output so I will just sort -r | tail -30 and the largest number is right above the new prompt. That way I don't have to worry about lines with the most hits going off the top of the screen.
  • MultiplyByZer0
    MultiplyByZer0 over 5 years
    And if you want to remove the item count, add a pipe to cut -c9- at the end.