Question

I need to find the shortest solution for

I have a file with lines:

str \t numbers \t str

I need to output 10 most popular number values

for instance with input:

qwqe    128.10.189.128  wwewe
wwewe   228.74.165.218  tssht
dgerg   15.46.11.247    cvbcb
ddfdfdf 205.219.171.189 ggghg
sds 228.5.220.225   ggbg
hg  110.139.130.107 vb
asd 130.139.130.107 vggh
sdsd    66.207.133.81   gff
q   13.26.210.115   f
ggsgfgdfzgg 42.186.57.170   ffdd
dfdf    196.246.43.169  dfdf
sdsd    228.5.220.225   ggsdg
asd 130.139.130.107 vggh
sdsd    66.207.133.81   f
sds 228.5.220.225   ggbg
sdsd    66.207.133.81   gff
sds 228.5.220.225   ggbg
asd 130.139.130.107 vggh
asd 130.139.130.107 vggh
asd 130.139.130.107 vggh
sdsd    66.207.133.81   gff
sdsd    66.207.133.81   gff
sdsd    66.207.200.81   gff

the expected output is:

66.207.133.81
130.139.130.107    
228.5.220.225
66.207.200.81
42.186.57.170
228.74.165.218
205.219.171.189
196.246.43.169
15.46.11.247

I can do this with this sequence of commands:

cut -d $'\t' -f2 file.txt|sort|uniq -c|sort -r|head|cut -c6-

but this seems complicated and I am not sure it is the shortest way to do it

Was it helpful?

Solution

It wouldn't save characters, but you can eliminate the initial cut if you sort and uniq -c by field:

sort -t $'\t' -k2 file.txt | uniq -f2 -s1 -c

That at least removes a command from the chain. You could also combine the last head and cut with a simple awk one-liner:

awk '{if(NR<11)print $3}'

This is both longer and less simple, but again saves a command.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top