awk: print columns depending on their number

hi guys,

i have the following problem: i have a matrix with 3 columns and over 450 rows like this:

0.0165    0.0151    0.0230
0.0143    0.0153    0.0134
0.0135    0.0123    0.0195
0.0173    0.0153    0.0182

i now want to calculate the average of every line and divide every element of this line by its corresponding average. although a complete stranger to bash and/or awk i somehow figured out how to do this (probably in a strange way; see code)

awk '{ sum=0; for (i=1; i<=NF; i++) sum += $i; average= sum/NF; for (j=1; j<=NF; j++) print $j/average; }' data.txt 

the problem now is, that i get all the results in just one column. of course i could just say

 ... print $1, $2, $3... 

but since the matrices vary in their number of columns this won't be a good way.

any thoughts?

waddle

change this:

print $j/average

to that:

printf("%.4f%c", $j/average, (j==NF)?ORS:FS)
1 Like