linux - Find the most common line in a file in bash -


i have file of strings:

string-string-123 string-string-123 string-string-123 string-string-12345 string-string-12345 string-string-12345-123 

how retrieve common line in bash (string-string-123)?

you use awk this:

awk '{++a[$0]}end{for(i in a)if(a[i]>max){max=a[i];k=i}print k}' file 

the array a keeps count of each line. once file has been read, loop through , find line maximum count.

alternatively, can skip loop in end block assigning line during processing of file:

awk 'max < ++c[$0] {max = c[$0]; line = $0} end {print line}' file 

thanks glenn jackman useful suggestion.


it has rightly been pointed out 2 approaches above print out 1 of occurring lines in case of tie. following version print out of occurring lines:

awk 'max<++c[$0] {max=c[$0]} end {for(i in c)if(c[i]==max)print i}' file 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -