Given this sample input:
ID Sample1 Sample2 Sample3 One 10 0 5 Two 3 6 8 Three 3 4 7
I needed to produce this output using AWK:
ID Sample1 Sample2 Sample3 One 62.50 0.00 25.00 Two 18.75 60.00 40.00 Three 18.75 40.00 35.00
This is how I solved it:
function percent(value, total) {
return sprintf("%.2f", 100 * value / total)
}
{
label[NR] = $1
for (i = 2; i <= NF; ++i) {
sum[i] += col[i][NR] = $i
}
}
END {
title = label[1]
for (i = 2; i <= length(col) + 1; ++i) {
title = title "\t" col[i][1]
}
print title
for (j = 2; j <= NR; ++j) {
line = label[j]
for (i = 2; i <= length(col) + 1; ++i) {
line = line "\t" percent(col[i][j], sum[i])
}
print line
}
}
This works fine in GNU AWK (awk
in Linux, gawk
in BSD),
but not in BSD AWK, where I get this error:
$ awk -f script.awk sample.txt awk: syntax error at source line 7 source file script.awk context is sum[i] += >>> col[i][ <<< awk: illegal statement at source line 7 source file script.awk awk: illegal statement at source line 7 source file script.awk
It seems the problem is with the multidimensional arrays. I'd like to make this script work in BSD AWK too, so it's more portable.
Is there a way to change this to make it work in BSD AWK?