2

I have a file logs.txt with numbers which repeat:

1   QWE
1   ASD 
1   QWE
1   QWE
1   QWE
1   qwe
12
22  qwe
2   aaa
2   vcc
2   xxx
4   asa
44  qwe
4   gfd
4   bcx
5
6   kuy
7
76  lol
5
4   ggg
23
2   ttt
34
5
5
76
5
6
78
99
8
88
7
6
88
88
88
88  wer
88  tyu
99  dsf
78  dfg
78  fgh
78
78  qwe
6   qwe
5
22  qwe 

I need to sort them. So the highest should be on top. example: number 1 repeats 20 times, number2 repeats 44 times...

20 1
44 2
1  3
5  4 
.....

I run this commands:

awk '{print $1}' logs.txt >> log2.txt

cat log2.txt | uniq -c >> log3.txt

sort -n srp >> log4.txt

tac log4.txt > log5.txt

It works for me but it took much time! I want to put all commands in the file to execute. I need to create a script!

  • 1
    Your requirements ("highest should be on top") don't match with what you show as result where the lowest comes first. - Please clarify; shall the first or second column be sorted? – Janis Jun 2 '15 at 4:37
3

You only need:

sort -rnk1 file | awk '{print $1}' | uniq -c | sort -nk2

or if you only have number in first field:

sort -rnk1 file | tr -cs 0-9 '[\n*]' | uniq -c | sort -nk2
  • No it gives me not the result which I want. I think we need to change tr -cs 0-9 '[\n*]' thing. – Invoker Jun 2 '15 at 4:10
  • @Invoker: What did you get when running above command? – cuonglm Jun 2 '15 at 4:16
  • @Invoker; Note that @cuonglm's suggestion creates (with your sample data) equivalent results if compared to the results of the suggestion in my own answer. So I assume that both our solutions work correctly. Please re-inspect your data source. – Janis Jun 2 '15 at 4:35
  • well it works but how to make it now from the most repeating to less repeating(example: number 1 repeats 20 time , number 2 repeats 15 times, number 3 repeats 10 times. . .sort them according to repeating score. Number which had been repeating most should be on top – Invoker Jun 2 '15 at 5:03
  • @Invoker: Use sort -rnk1 instead of sort -nk2 – cuonglm Jun 2 '15 at 5:08
3

Here is another solution, based just on awk and sort:

awk '{a[$1]++} END{for(i in a)print a[i],i}' logs.txt | sort -k2n
  • yes it works but how to sort (example: number 1 repeats 20 time , number 2 repeats 15 times, number 3 repeats 10 times. . .)? sort them according to repeating score. Number which had been repeating most should be on top. – Invoker Jun 2 '15 at 5:04
  • @Invoker; Then sort on the first key, and reverse the sorting: sort -k1nr. – Janis Jun 2 '15 at 5:18
3

GNU awk allows array traversal order control via PROCINFO["sorted_in"], so

awk 'BEGIN{PROCINFO["sorted_in"] = "@val_num_desc"}; {a[$1]++}; 
END{for (k in a) print k, a[k]}' logs.txt
  • No it shows me not what I want. – Invoker Jun 2 '15 at 5:09
  • @Invoker, in that case you perhaps do not have GNU awk. What does awk --version say? – iruvar Jun 2 '15 at 5:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.