Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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!

share|improve this question
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 at 4:37

3 Answers 3

up vote 3 down vote accepted

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
share|improve this answer
    
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 at 4:10
    
@Invoker: What did you get when running above command? –  cuonglm Jun 2 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 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 at 5:03
    
@Invoker: Use sort -rnk1 instead of sort -nk2 –  cuonglm Jun 2 at 5:08

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
share|improve this answer
    
No it shows me not what I want. –  Invoker Jun 2 at 5:09
    
@Invoker, in that case you perhaps do not have GNU awk. What does awk --version say? –  1_CR Jun 2 at 5:11

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
share|improve this answer
    
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 at 5:04
    
@Invoker; Then sort on the first key, and reverse the sorting: sort -k1nr. –  Janis Jun 2 at 5:18
    
Yeah it works too Thank you!) –  Invoker Jun 2 at 5:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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