Write the frequency of each number in column into next column in bash

Viewed 47

I have a column with different numbers in each row in a text file. Now, I want the frequency of each number into a new column. And the similar rows should be deleted, to have only each unique number in the first column and the frequency in the second column.

Input:

0.32832977
0.31647876
0.31482627
0.31447645
0.31447645
0.31396809
0.31281157
0.312004
0.31102326
0.30771822
0.30560062
0.30413213
0.30373717
0.29636685
0.29622422
0.29590765
0.2949896
0.29414582
0.28841901
0.28820667
0.28291832
0.28243792
0.28156429
0.28043638
0.27872239
0.27833349
0.27825573
0.27669023
0.27645657
0.27645657
0.27645657
0.27645657

Output:

0.32832977 1
0.31647876 1
0.31482627 1
0.31447645 2
0.31396809 1
0.31281157 1
0.312004   1
0.31102326 1
0.30771822 1
0.30560062 1
0.30413213 1
0.30373717 1 
0.29636685 1
0.29622422 1
0.29590765 1
0.2949896  1
0.29414582 1
0.28841901 1
0.28820667 1
0.28291832 1
0.28243792 1
0.28156429 1
0.28043638 1
0.27872239 1
0.27833349 1
0.27825573 1
0.27669023 1
0.27645657 4

I tried this command, but it doesn't seem to work:

awk -F '|' '{freq[$1]++} END{for (i in freq) print freq[i], i}' file
2 Answers

Using Awk is an overkill IMO here, the built-in tools will do the work just fine:

sort -n file | uniq -c | sort

Output:

1 0.32832977

2 0.31447645

4 0.27645657

For completeness, this would be the awk solution (no need to set the input field separator to | if your sample input is representative).

awk '{f[$0]++} END{for (i in f) print i, f[i]}' input.txt
0.28820667 1
0.30560062 1
0.312004 1
0.28156429 1
0.28291832 1
0.29636685 1
0.31447645 2
0.30373717 1
0.31482627 1
:

You can, however, set the output field separator to | or (as I did here) to a tab character, to format the output

awk '{f[$0]++} END{OFS="\t"; for (i in f) print i, f[i]}' input.txt
0.28820667  1
0.30560062  1
0.312004    1
0.28156429  1
0.28291832  1
0.29636685  1
0.31447645  2
0.30373717  1
0.31482627  1
:
Related