Marty Wallace
Marty Wallace

Reputation: 35744

Order text file by frequency of occrrences

Given a text file which will have duplicate lines in it such as:

this is a line
this is a line
this is another line
this is a line
this is yet another line
this is yet another line

Is it possible on the command line to print out each unique line but ordered by the frequency of which it appears.

I.e. the result of the previous text would be:

this is a line
this is yet another line
this is another line

The they appear 3, 2 and 1 times respectively.

Upvotes: 2

Views: 251

Answers (2)

smeso
smeso

Reputation: 4295

Try with this:

sort file|uniq -c|sort -rn

EDIT: Also if you want to delete the counter at the beginning of the lines just pipe sed 's/^\s*[0-9]* \(.*\)$/\1/' at the end of the above command.

Upvotes: 4

Jotne
Jotne

Reputation: 41450

You can do like this:

awk '{ a[$0]++ } END {for (i in a) print a[i], i }' | sort -nr
3 this is a line
2 this is yet another line
1 this is another line

Upvotes: 1

Related Questions