Skip to content

Instantly share code, notes, and snippets.

Created March 27, 2012 00:34
Show Gist options
  • Save anonymous/2211032 to your computer and use it in GitHub Desktop.
Save anonymous/2211032 to your computer and use it in GitHub Desktop.
#!/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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment