tl;dr sort myfile.log | uniq -c | sort -n -r

I wanted to count recurring lines in a log file and started writing a complicated Python script but then wondered if I can just do it with bash basics.
And after some poking and experimenting I found a really simple one-liner that I'm going to try to remember for next time:

You can't argue with the nice results :)

cat myfile.log
one
two
three
one
two
one
once
one

▶ sort myfile.log | uniq -c | sort -n -r
   4 one
   2 two
   1 three
   1 once

Comments

Your email will never ever be published.

Previous:
Find the largest node_modules directories with bash September 30, 2022 Linux, Bash, macOS
Next:
First impressions trying out Rome to format/lint my TypeScript and JavaScript November 14, 2022 Node, JavaScript
Related by category:
hylite as an executable October 15, 2025 Linux
gg2 - a new CLI for helping me manage git branches August 6, 2025 macOS
How to count the number of non-blank lines with Bash September 3, 2025 Bash, Linux, macOS
Find the source of an alias in bash September 29, 2025 Bash, Linux, macOS
Related by keyword:
Find the source of an alias in bash September 29, 2025 Linux, Bash, macOS
gg shell completion August 13, 2025 Linux, JavaScript, Bun, macOS
set -ex - The most useful bash trick of the year August 31, 2014 Linux
How to sort case insensitively with empty strings last in Django April 3, 2022 Python, Django, PostgreSQL