Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use awk to calculate sum for multiple columns

Tags:

awk

I have a list with 8 columns, in which the first 6 are the same.

6   99999715    99999771    NM_001013399    0   -   23  0.0714286
6   99999715    99999771    NM_001013399    0   -   24  0.0178571
6   99999715    99999771    NM_001013399    0   -   25  0.1250000

I need to calculate the average for column 7 and column8, as well as $7*$8, and get the format like:

6   99999715    99999771    NM_001013399    0   -   ave($7) ave($8) ave($7*$8)

How should I do this? thx

like image 323
LookIntoEast Avatar asked May 05 '12 17:05

LookIntoEast


1 Answers

Haven't tried it, but it should be just:

{sum7+=$7; sum8+=$8; mul+=$7*$8} END {print sum7/NR,sum8/NR,mul/NR}

In response to popular demand, I'll add the printf.

{sum7+=$7; sum8+=$8; mul+=$7*$8}
END {printf "%s %4i %10.7f %10.7f\n", substr($0,0,49),sum7/NR,sum8/NR,mul/NR}
like image 136
stark Avatar answered Sep 22 '22 11:09

stark