I'm trying to transform some data to a percent of the total of each column, very similar to this thread except that I need to do this for every column:Calculate and divide by total with AWKData would like something like this (but more columns and rows):ID     Sample1     Sample2      Sample3One      10          0            5Two      3           6            8Three    3           4            7And the desired output would look like this:ID     Sample1     Sample2     Sample3One     62.50        0.0        25.0Two     18.75       60.0        40.0Three   18.75       40.0        35.0   The following works for a single column, but I want to do this forevery column, except the first one.gawk -F\t '{a[NR]=$1;x+=(b[NR]=$2)}END{while(++i<=NR)print a[i]\t100*b[i]/x}' file.txt Thanks a lot for any help you might be.
How to calculate percent of every column?
awk
Not 100% the same output as you asked, but hopefully close enough: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    }}Produces output:ID    Sample1 Sample2 Sample3One   62.50   0.00    25.00Two   18.75   60.00   40.00Three 18.75   40.00   35.00Run it with gawk -f script.awk file.txtOf course, you could flatten the script to a single line,but I think it's better to keep it in a script file like this so it's easier to read and maintain.A simpler and better version that works with BSD AWK too, not only GNU AWK:function percent(value, total) {    return sprintf(%.2f, 100 * value / total)}BEGIN { OFS = \t }NR == 1 { gsub(/ +/, OFS); print; next }{    label[NR] = $1    for (i = 2; i <= NF; ++i) {        sum[i] += col[i, NR] = $i    }}END {    for (j = 2; j <= NR; ++j) {        $1 = label[j]        for (i = 2; i <= NF; ++i) {            $i = percent(col[i, j], sum[i])        }        print    }}