[Techtalk] sum in awk-problem

Gebhard Dettmar gebhard.dettmar at student.hu-berlin.de
Tue Sep 21 11:07:22 EST 2004


Dear techtalkers

I have following problem in awk: given this inputfile contact
---snip---
ACTION_FALLSTUDIEN 26
ACTION_HOME 97
ACTION_HOME1 14
ACTION_HOME_WEITERLEITUNG 1
ACTION_INDEX 79
---snap---
I want to sum $2 and then compute the share of each line on that sum.
But
awk '{sum += $2; print $1, $2 / sum * 100}' contact
sums up every line and performs the division after each addition and 
awk '{sum += $2} END {print $1, $2 / sum * 100}' contact
just gives me the last line. How can I store sum = 217 and then divide
each line's $2 only by that value
Any help would be appreciated
Regards gebhard




More information about the Techtalk mailing list