[Techtalk] sum in awk-problem

Riccarda Cassini riccarda.cassini at gmx.de
Tue Sep 21 14:15:07 EST 2004


Gebhard Dettmar wrote:
> 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.

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...

Ciao,
Riccarda



-- 
+++ GMX DSL Premiumtarife 3 Monate gratis* + WLAN-Router 0,- EUR* +++
Clevere DSL-Nutzer wechseln jetzt zu GMX: http://www.gmx.net/de/go/dsl



More information about the Techtalk mailing list