7 October 2022 0 comments Linux, MacOSX, Bash
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
- Previous:
- Find the largest node_modules directories with bash 30 September 2022
- Next:
- First impressions trying out Rome to format/lint my TypeScript and JavaScript 14 November 2022
- Related by category:
- Linux tip: du --max-depth=1 27 September 2007 Linux
- How to create-react-app with Docker 17 November 2017 Linux
- Be very careful with your add_header in Nginx! You might make your site insecure 11 February 2018 Linux
- set -ex - The most useful bash trick of the year 31 August 2014 Linux
- How to intercept and react to non-zero exits in bash 23 February 2023 Bash
- Related by keyword:
- set -ex - The most useful bash trick of the year 31 August 2014
- How to intercept and react to non-zero exits in bash 23 February 2023
- Run something forever in bash until you want to stop it 13 February 2018
- In Python you sort with a tuple 14 June 2013
- Sorting mixed type lists in Python 3 18 January 2014