Reputation: 767
I want to write an script which gets the average of two column of a file where the first column is equal, I did that with awk. it works, but the in the output there was exponential format data
121323e+2
then I changed the print to printf and I added %f for the float
awk '
NR>1{
arr[$1] += $2
arr2[$1] += $3
count[$1] += 1
}
END{
for (a in arr) {
printf "%4.3f", a " " arr[a] / count[a] " " arr2[a] / count[a]
}
}
' t1.txt > t2.txt
I have problem with this part:
printf "%4.3f", a " " arr[a] / count[a] " " arr2[a] / count[a]
the output:
0001204.0001125.0001118.0001053.0001046.0001039.000901.000822.000815.000808.000750
which I wanted
0001 204.000 1125.000
1118.000 1053.000 1046.000
1039.000 901.000 822.000
815.000 808.000 750
Upvotes: 0
Views: 466
Reputation: 74605
Assuming that the rest of the code does what you want, you should change the printf
to something like this:
printf "%4.3f %4.3f %4.3f\n", a, arr[a] / count[a], arr2[a] / count[a]
Here I've included one format specifier for each value to be inserted and added a newline \n
to the end. Each value is then separated by commas.
Upvotes: 2