[Techtalk] sum in awk-problem

Gebhard Dettmar gebhard.dettmar at student.hu-berlin.de
Tue Sep 21 15:59:21 EST 2004


techtalk-bounces at linuxchix.org scribbled on Tuesday, September 21,
2004 2:15 PM

> Gebhard Dettmar wrote:
>> [...]
> The following script should do the job if your data set isn't
> excessively large (all data needs to be kept in memory for the
> second pass): 
> 
> #!/bin/awk -f
> 
> { sum += $2;  line[n++] = $0 }
> 
> END {
>   print "total sum =", sum;
> 
>   for (i=0; i<n; i++) {
>     split(line[i], f);
>     printf("%-25s %6.2f : %5.2f%%\n", f[1], f[2], f[2] / sum *
> 100);   } }
> 
> It would print:
> 
> total sum = 217
> ACTION_FALLSTUDIEN         26.00 : 11.98%
> ACTION_HOME                97.00 : 44.70%
> ACTION_HOME1               14.00 :  6.45%
> ACTION_HOME_WEITERLEITUNG   1.00 :  0.46%
> ACTION_INDEX               79.00 : 36.41%
> 
> The idea is just to store the original lines while going through the
> input data, so you still have them at the end, when the sum has
> been determined... 
Fantastic ;-))) That was the crucial point. Thanks a lot!
> Ciao,
> Riccarda
Cya
Gebhard



More information about the Techtalk mailing list