Skip to content

Instantly share code, notes, and snippets.

Created March 27, 2012 00:34
Show Gist options
  • Select an option

  • Save anonymous/2211032 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/2211032 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Mar 27, 2012.
    32 changes: 32 additions & 0 deletions average.awk
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #!/bin awk -f

    BEGIN {
    FS=","
    rows[""]=0
    rowsc[""]=0
    labels[""]=0
    }
    {
    if (NR == 1) { header=$0; cols=NF; }
    else if ($0 != header) {
    labels[$1]++
    for (i=2; i<=NF; i++) {
    rows[$1 " " i] += $i
    rowsc[$1 " " i]++
    }
    }
    }
    END {
    print header
    for (label in labels) {
    if (label == "") continue
    line=label
    for (i=2; i<=cols; i++) {
    if (rowsc[label " " i]) {
    avg=rows[label " " i]/rowsc[label " " i]
    line=sprintf("%s,%8.4f", line, avg)
    }
    }
    print line
    }
    }