help with adding up numbers

I have a file which has following contents which I want to add up.

28170.24
28170.24
28170.24
28170.24
28170.24
28170.24
28170.24
28170.24
28170.24
28170.24
28170.24
28170.24
28170.24
28170.24
139038.72
139038.72
139038.72
139038.72
139038.72
139038.72
139038.72
139038.72
139038.72

Now when I use

paste -s -d+ <file-name>

it gives the string that I am looking for.

+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+21125.12+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+21125.12+14080.00+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24+28170.24

But when I give

paste -s -d+ <file-name> | bc

I do not get any output. :wall:

Even for loop fails with error..

bash: ((: 139038.72: syntax error in expression (error token is ".72")
bash: ((: 139038.72: syntax error in expression (error token is ".72")
bash: ((: 139038.72: syntax error in expression (error token is ".72")
bash: ((: 139038.72: syntax error in expression (error token is ".72")

inputFile

2
3
4
5

Command

awk 'BEGIN{sum=0} {sum += $1} END{print sum}' inputFile

. Output from the command is 14.

You can also try adding scale

 e.g.  scale=2 

to the command.

Thanks Sheel. Was getting some output with that but did not try with the scale option.
Even the scale option isn't helping either.

awk 'BEGIN {scale=6} {s+=$0} END {printf "%s",s}' test2
2.716e+07

is what I get.
I want it to be in the correct format.

use

%d or %.2f 
1 Like

Awww..!!! How could I not do that :cry:

Thanks Sheel...

Regarding the 1st post, there is a leading + in the paste output that should not be there. Is the first line in your input file empty? Remove the empty line...