Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding rows and columns in AWK

Status
Not open for further replies.

ateleco

Technical User
Dec 8, 2003
2
US
I have a chart that I need to add the rows and the columns. I have a script that can do ether but I can not get it to work togetter.
The chart looks like this The 1st row is the max for each columns:

25 30 35 21 30
Name1 10 24 34 17 28
Name 2 15 23 32 17 22
Name 3 23 26 23 21 29

Now I need to add the rows and the columns and get the avg for each.

Thanks for any help.
 
I am also learning awk programming, just give you a stupid script for reference only.

NF ~ 5 {
print $0
}
NF ~ 7 {
row_tot = $3 + $4 + $5 + $6 + $7
print $0, row_tot / 5
for (i=3;i<=NF;i++)
col_tot += $i
}
END {
printf &quot;%9d %d %d %d %d\n&quot;, col_tot[3] / 3, col_tot[4] / 3, col_tot[5] / 3, col_
tot[6] / 3, col_tot[7] / 3
}

tikual
 
Try something like this:
Code:
NR==1{nf=NF;print $0&quot;\tAVG&quot;;next}
NF>nf{++nr;tr=0;f=NF-nf+1
 for(i=f;i<=NF;++i){tr+=$i;tc[i]+=$i}
 print $0&quot;\t&quot;tr/nf
}
END{printf &quot;AVG &quot;
 for(i=f;i<f+nf;++i)printf &quot; &quot;tc[i]/nr
 printf &quot;\n&quot;
}
I let you play with formatting ...

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top