shellsortingawkbc

How to print float numbers with awk


I have this input file:

Adrian 20.6
Bruce 40.2
Adrian 30.4
Janick 24.2
Dave 42.7
Bruce 42.9
Janick 41.2

And with a answer I've got here, I was able to sum the numbers, but now the numbers are float and I don't know how to print them.

For now, my output is:

Adrian 50
Bruce 82
Dave 42
Janick 65

But it should be with the decimals as well.

$(awk '{sum[$1] += $2} END {for (i in sum) print i, sum[i]}' $arq > temp.txt && sort temp.txt)

I've made that code to sort by alphabetical order.


Solution

  • You're in a locale where the decimal separator is not . so the . is being treated as any other character that's not part of a number (e.g. a or @) and so your numbers are being truncated to the part before the ..

    Do LC_ALL=C awk '...' to set your locale to one that does use a . for the decimal separator and then your script will work as-is.