counts based on percentage

I have a file with multiple entries and I have calculated the percentages. Now I want to know how many of my entries are there between 1-10% 11-20% and so on..

chr1_14401_14450    0.211954217888936
chr1_14451_14500   1.90758796100042
chr1_14501_14550   4.02713013988978
chr1_14551_14600   6.14667231877914
chr1_14601_14650   8.2662144976685
chr1_14651_14700   10.3857566765579
chr1_14701_14750   12.5052988554472
chr1_14751_14800   14.6248410343366
chr1_14801_14850   16.7443832132259
chr1_14851_14900   18.8639253921153

My percentages are in double,so it needs to be rounded off to the nearest and then accordingly calculate. How can I do this in awk?

o/p

0-10 5
11-20 5
21-30 0
31-40 0

so on until
90-100% 0

Thanks,

awk '{a[int($2/10)*10]++}END{for (i in a) print i "-" i+10, a|"sort -n"}' infile

Some updates based on Chubler_XL's code below

awk '{a[int($2/10)*10]++}END{for (i=0;i<100;i+=10) print i+1 "-" i+10, 0+a}' infile
1 Like

Slight improvement - ensure zero counts are printed, no need to call external sort:

awk '{a[int($2/10)]++}END{while(++i<11) print i*10-9 "-" i*10, 0+a[i-1]}' infile
2 Likes

Hello,

Thank you..It works great..

But what if I want to have it for every 1 %, I mean instead of 1-10,11-20 and so on.. I want for 0-1,1-2,2-3 so on.. 99-100?

I tied to change the code accordingly, but it gave me zero's.

Regards,

---------- Post updated at 09:46 AM ---------- Previous update was at 09:15 AM ----------

I figured it out.

Thank you for the response