2

I'm trying to figure out how to count the number of occurrences of values that are greater than some number (i.e. 0.1) in each column and then print that value out for each column. Here would be some sample data, except for this would need to be general, i.e. work for any number of columns:

Data     Sample1     Sample2     Sample3
row1      0.05         0.12        5.00 
row2      0.00         5.00        8.75
row3      1.19         0.00        6.78

And then the desired output could be something like (counting the numbers greater than 0.1):

Sample1:  1
Sample2:  2
Sample3:  3

Perl, awk, or coreutils would be fine, I just need to build it into a bash script to output some stats on the table of data.

I can count one column, but I don't know how to do it for all of them:

gawk -F"\t" 'NR>1 {if ($2>0.1) print $1;}' | wc -l

Thanks in advance for the help.

3

You are almost there, just do it in the loop:

awk '{for(i=2;i<=NF;i++){if(NR==1)h[i]=$i;else if($i>0.1)x[i]++}}END{for(i in x){print h[i]": "x[i]}}'
  • Thanks jimmij. So in order to get the column header from the file instead of printing "Sample" as above, that would have to be read into a different array? – jpalmer Dec 22 '14 at 22:27
  • Maybe like this: gawk 'NR==1{for(i=1;i<=NF;i++)h[i]=$i;} NR>1{for(i=2;i<=NF;i++){if($i>0.1)x[i]++}}END{for(i in x){print h[i]": "x[i]}}' – jpalmer Dec 22 '14 at 22:33
  • @jpalmer Yes, it can be written a little bit shorter, see the update. – jimmij Dec 22 '14 at 22:37

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.