arrays - Search pattern and print hits lower than threshold -
arrays - Search pattern and print hits lower than threshold -
here illustration need:
input:
a 5 7 11 b 10 b 11 b 12 . . . output:
a 2 b 0 so on output should hits lower threshold (in case $2 < 10).
my code is:
awk 'ofs="\t" {v[$1]+=$2; n[$1]++} end {for (l in n) {print l, n[l]} }' input and output
a 3 b 3 i not sure set status $2 < 10.
you can check threshold status $2 < value, value awk variable given -v value=xx.
also, using v[$1]+=$2: sums, not counts matching cases.
all together, utilize this:
awk -v t=10 '{list[$1]} $2<t {count[$1]++} end {for (i in list) print i, count[i]+0}' file note need utilize 2 arrays: 1 maintain track of counters , 1 maintain track of possible values.
explanation-v t=10 provide threshold. {list[$1]} maintain track of possible first fields appearing. $2<t {count[$1]++} if 2nd field smaller threshold, increment counter. end {for (i in list) print i, count[i]+0} finally, loop through first fields , print number of times had value lower threshold. count[i]+0 trick makes print 0 if value not set. test $ awk -v t=10 '{list[$1]} $2<t {count[$1]++} end {for (i in list) print i, count[i]+0}' a 2 b 0 arrays awk count
Comments
Post a Comment