I think you are closer than you think, but your problems stem from not handling putting the pieces together in the right order. By default awk
handles floating-point math. Your summation of sum[$1] += $2
will be done with floating point numbers if your input numbers contains decimal places.
Why you are using a Command Substitution, e.g. $(..)
is unclear. That isn't needed to fill temp.txt
with the sorted sums from your input. Let's take for example your input file in a file named input
. Then to sum and output the values you need nothing more than:
awk '{sum[$1] += $2} END {for (i in sum) print i, sum[i]}' input
Which results in the output of:
Bruce 83.1
Dave 42.7
Adrian 51
Janick 65.4
To sort the results, simply pipe the awk
output to sort
, e.g.
awk '{sum[$1] += $2} END {for (i in sum) print i, sum[i]}' input | sort
Result:
Adrian 51
Bruce 83.1
Dave 42.7
Janick 65.4
Now, to fill temp.txt
, you simply need to redirect the output to temp.txt
, e.g.
awk '{sum[$1] += $2} END {for (i in sum) print i, sum[i]}' input | sort > temp.txt
temp.txt
now contains the sorted output. To nicely tabularize the output, you can format the output by saving the max length for $1
and setting the name field-width to max
and then outputting the floating-point number in a fixed format (total floating-point field-width 10
with 1
digit following the decimal point), e.g.
{sum[$1] += $2; len=length($1); if(len>max)max=len} ... printf "%-*s%10.1f\n", max, i, sum[i]
Or in full-form:
awk '{sum[$1] += $2; len=length($1); if(len>max)max=len} END {for (i in sum) printf "%-*s%10.1f\n", max, i, sum[i]}' input | sort > temp.txt
With the results in temp.txt
now being:
$ cat temp.txt
Adrian 51.0
Bruce 83.1
Dave 42.7
Janick 65.4