awk counting number of digits within a given range -


how can count number of times digit within given range of numbers in field occurs?

for example, raw text foo.txt shown below:

2,3,4,2,4 2,3,4,32,4 2,3,4,12,4 2,3,4,4,4 2,3,4,,4 2,3,4,15,4 2,3,4,15,4 

i want count number of times digit in field #4 falls between following ranges: [0,10) , [10,20), lower bound inclusive , upper bound not.

the result should be:

range 0-10: 2 range 10-20: 3

here awk code below, getting 8600001 both ranges, awk -f prog.awk foo.txt:

#!/usr/range/awk # prog.awk  begin {     fs=",";     $range1=0;     $range2=0; } $4 ~ /[0-9]/ && $4 >= 0 && $4 < 10 { $range1 += 1 }; $4 ~ /[0-9]/ && $4 >= 10 && $4 < 20 { $range2 += 1 }; end {     print $range1, "\t", $range2; } 

another awk

$ awk -f, '$4>=0{a[int($4/10)]++}               end{print "range 0-10:" a[0],"range 10-20:" a[1]}' file  range 0-10:2 range 10-20:3 

can expanded cover full range

$ awk -f, '$4>=0{a[int($4/10)]++}               end{for(k in a) print "range ["k*10"-"(k+1)*10"):", a[k]}' file  range [0-10): 2 range [10-20): 3 range [30-40): 1 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -