VDC
VDC

Reputation: 1449

Count the frequency of a number in a file with awk

I have the following file:

1
1
2
3
3
3
4
4
5
5
5
5

I would like to count how many times a number appear and how many times this is frequent: for example, the number 1 appear 2 times, the number 2 one time, the number 3 three times, the number 4 two times the 5 four times; the output would be a two columns file in which the first column how many times the number appear in the column, the second column represent the times in which a number is repeated, :

2 2  %(because the number 1 and number 4 appear 2 times and there are only 2 number that appear this often)
1 3
1 1
1 4

I hope that the output example file can help to understand...

Upvotes: 1

Views: 4938

Answers (2)

Kent
Kent

Reputation: 195109

this line should give you the result:

awk '{a[$0]++}END{for(x in a)b[a[x]]++;for(x in b)print b[x], x}' file

with your data:

kent$  cat file
1
1
2
3
3
3
4
4
5
5
5
5

kent$  awk '{a[$0]++}END{for(x in a)b[a[x]]++;for(x in b)print b[x], x}' file
1 4
1 1
2 2
1 3

Upvotes: 2

Benjamin Toueg
Benjamin Toueg

Reputation: 10867

uniq requires sorted input since it compares only consecutive lines:

uniq -c

So if not already sorted:

sort | uniq -c

The output for you given example would be:

  2 1
  1 2
  3 3
  2 4
  4 5

Upvotes: 6

Related Questions