awk - Average of a given number of rows -


i want average of number of rows, in case number dictated second column

-1 1 22.776109913596883 0.19607208141710716 -1 1 4.2985901827923954 1.0388892840309705 -1 1 4.642271812306717 0.96197712195674756 -1 2 2.8032298255711794 1.5930763994471333 -1 2 2.9358628368936479 1.5211062387604053 -1 2 4.9987168801017106 0.8933811184867273  1 4 2.6211673161014915 1.7037291934441456  1 4 4.483831056393683 0.99596956735821618  1 4 9.7189442154485732 0.4594901646050486 

the expected output

-1 1 0.732313 -1 2 1.33585  1 4 1.05306 

i have done

awk '{sum+=$4} (nr%3)==0 {print $2,$3,sum/3;sum=0;}' test 

which works, (somehow) generalize (nr%3)==0 in way awk realizes value of second column has changed , therefore means it's new average needs calculate. instance, first 3 rows have 1 value in second column, once 1 changes 2 means it's new average needs calculated.

does make sense?

try like:

awk '{sum[$2] += $4; count[$2] += 1; }      end { (k in sum) { print k " " sum[k]/count[k]; } }' 

not tested idea...

with method, whold computation printed @ end; may not want if input infinite stream, according example think should fine.

if want keep first column also, can same system.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -