awk - Use bash to cluster based on one column of a line -
the input below
a 20 240 15 150 b 65 210 b 80 300 c 90 400 c 34 320
for each category (labelled a,b,c..in 1st column), i'd find minimum maximum numbers (as biggest range). expect see:
a 15 240 b 65 300 c 34 400
so how using bash?
using awk:
awk ' !($1 in min) { min[$1] = $2; max[$1] = $3; next } { min[$1] = ( $2 < min[$1] ? $2 : min[$1] ) max[$1] = ( $3 > max[$1] ? $3 : max[$1] ) } end { for(x in min) print x, min[x], max[x] }' file 15 240 b 65 300 c 34 400
we iterate each line , assign min , max values map has first column key. in end block iterate hash , print out key , values both maps.
Comments
Post a Comment