-
-
Save colinmollenhour/2211036 to your computer and use it in GitHub Desktop.
Average multiple CSV files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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