7

Suppose I have a file similar to as follows:

Abigail 85
Kaylee 25
Kaylee 25
kaylee
Brooklyn
Kaylee 25
kaylee 25

I would like to find the most repeated line, the output must be just the line.

I've tried

sort list | uniq -c

but I need clean output, just the most repeated line (in this example Kaylee 25).

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
user1478993
  • 121
  • 2
  • 6

4 Answers4

7

Kaizen ~

$ sort zlist | uniq -c | sort -r | head -1|  xargs | cut -d" " -f2-

Kaylee 25

does this help ?

Nitin4873
  • 16,804
  • 1
  • 13
  • 15
  • I don't think the call to `xargs` is necessary here. – chepner Jun 04 '13 at 16:17
  • xargs .... its to supress the spaces , its plain and simple . you could also use translate if you liked !! – Nitin4873 Jun 04 '13 at 17:08
  • `xargs` will suppress valid consecutive spaces as well. You'll probably need to remove `xargs` and use some other method like `sed s/^\s\+[0-9]\+\s//'` to remove the count column instead of `cut` – Samveen Jun 04 '13 at 18:29
  • Use `sort --version-sort` instead of `sort -r`. For example, `--version-sort` will properly output "351, 51, 5", while `-r` will output "5, 351, 51". – fri Feb 24 '16 at 15:06
  • Note that this solution is wrong because the second `sort` doesn't have `-n` (pointed out by the [answer below](https://stackoverflow.com/a/26229009/)) – user202729 Aug 03 '19 at 09:04
4

IMHO, none of these answers will sort the results correctly. The reason is that sort, without the -n, option will sort like this "1 10 11 2 3 4", etc., instead of "1 2 3 4 10 11 12". So, add -n like so:

sort zlist | uniq -c | sort -n -r | head -1

You can then, of course, pipe that to either xargs or sed as described earlier.

Alex
  • 1,549
  • 2
  • 16
  • 41
2

awk -

awk '{a[$0]++; if(m<a[$0]){ m=a[$0];s[m]=$0}} END{print s[m]}' t.lis
jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
0
$ uniq -c list | sort -r | head -1 | awk '{$1=""}1'

Kaylee 25

Is this what you're looking for?

mproffitt
  • 2,409
  • 18
  • 24