Search code examples
linuxbashtr

Get most appear phrase (not word) in a file in bash


My file is

cat a.txt
a
b
aa
a
a a

I am trying to get most appear phrase (not word).

my code is

tr -c '[:alnum:]' '[\n*]' < a.txt | sort | uniq -c | sort -nr
      4 a
      1 b
      1 aa
      1

I need

2 a
1 b
1 aa
1 a a

Solution

  • sort a.txt | uniq -c | sort -rn