Adding row of numbers

is there another way of doing the below:

echo "7 3 8 2 2 1 3 83.4 8.2 4 8 73 90.5" | bc

shell is bash. os is linux and sunos.

bc seems to have an issue with long range of numbers

I (and bc) do not understand what you want to do here.

Are you adding?

$ echo "7 + 3 + 8 + 2 + 2 + 1 + 3 + 83.4 + 8.2 + 4 + 8 + 73 + 90.5" | bc
293.1

you have not given us or bc enough information in that command.

bc appears to be unreliable:

echo "15314.6 + 16320.6 + 15813.6 + 16835.6 + 19252.1 + 14823.5 + 16577.1 + 15068.1 + 13631 + 18158.1 + 17889.6 + 16835.6 + 15314.6 + 15314.6 + 16320.6 + 17358.6 + 15563.1 + 18158.1 + 18158.1 + 16835.6 + 17889.6 + 16577.1 + 2.53294e+08 + 17889.6 + 19252.1 + 17623.1 + 18975.6 + 12266 + 19530.6 + 18158.1 + 18158.1 + 17096.1 + 17623.1 + 19530.6 + 19252.1 + 21536.1 + 18428.6 + 19811.1 + 17096.1 + 19811.1 + 18428.6 + 18975.6 + 18158.1 + 18975.6 + 17096.1 + 19530.6 + 18158.1 + 16577.1 + 18701.1 + 16577.1 + 19811.1 + 16066.1 + 18158.1 + 14340.5 + 17623.1 + 17358.6 + 18701.1 + 16577.1 + 16320.6 + 18975.6 + 19252.1 + 16320.6 + 13398.5 + 18975.6 + 15563.1 + 17889.6 + 16835.6 + 14102 + 11183.5 + 18158.1 + 18158.1 + 17623.1 + 17623.1 + 14581 + 16835.6 + 16066.1 + 17889.6 + 14340.5 + 17889.6 + 18158.1 + 17889.6 + 16066.1 + 18701.1 + 20664.6 + 20093.6 + 18158.1 + 21830.7 + 17623.1 + 20093.6 + 17358.6 + 19811.1 + 19530.6 + 19530.6 + 18975.6 + 18975.6 + 19530.6 + 19252.1 + 20953.1 + 20378.1 + 17889.6 + 17889.6 + 17358.6 + 19252.1 + 18975.6 + 18158.1 + 16835.6 + 18158.1 + 18975.6 + 14340.5 + 16835.6 + 15813.6 + 19811.1 + 17096.1 + 17889.6 + 14102 + 15813.6 + 19252.1 + 18158.1 + 16577.1 + 20093.6 + 20378.1" | bc
(standard_in) 1: syntax error

bc does not support exponential notation.

Regards,
Alister

nope, just doesn't understand what your giving it:

$ echo "2.53294e+08 + 5" |bc
(standard_in) 1: syntax error

The "e+" (exponential) notation is a bit to complicated for bc. Convert these first to decimal like "253294000" and try again:

$ echo "15314.6 + 16320.6 + 15813.6 + 16835.6 + 19252.1 + 14823.5 + 16577.1 + 15068.1 + 13631 + 18158.1 + 17889.6 + 16835.6 + 15314.6 + 15314.6 + 16320.6 + 17358.6 + 15563.1 + 18158.1 + 18158.1 + 16835.6 + 17889.6 + 16577.1 + 253294000 + 17889.6 + 19252.1 + 17623.1 + 18975.6 + 12266 + 19530.6 + 18158.1 + 18158.1 + 17096.1 + 17623.1 + 19530.6 + 19252.1 + 21536.1 + 18428.6 + 19811.1 + 17096.1 + 19811.1 + 18428.6 + 18975.6 + 18158.1 + 18975.6 + 17096.1 + 19530.6 + 18158.1 + 16577.1 + 18701.1 + 16577.1 + 19811.1 + 16066.1 + 18158.1 + 14340.5 + 17623.1 + 17358.6 + 18701.1 + 16577.1 + 16320.6 + 18975.6 + 19252.1 + 16320.6 + 13398.5 + 18975.6 + 15563.1 + 17889.6 + 16835.6 + 14102 + 11183.5 + 18158.1 + 18158.1 + 17623.1 + 17623.1 + 14581 + 16835.6 + 16066.1 + 17889.6 + 14340.5 + 17889.6 + 18158.1 + 17889.6 + 16066.1 + 18701.1 + 20664.6 + 20093.6 + 18158.1 + 21830.7 + 17623.1 + 20093.6 + 17358.6 + 19811.1 + 19530.6 + 19530.6 + 18975.6 + 18975.6 + 19530.6 + 19252.1 + 20953.1 + 20378.1 + 17889.6 + 17889.6 + 17358.6 + 19252.1 + 18975.6 + 18158.1 + 16835.6 + 18158.1 + 18975.6 + 14340.5 + 16835.6 + 15813.6 + 19811.1 + 17096.1 + 17889.6 + 14102 + 15813.6 + 19252.1 + 18158.1 + 16577.1 + 20093.6 + 20378.1" | bc

255411071.5

i dont want to have to convert anything. can another program handle this? possibly awk

AWK does support exponential notation.

perl can do it

perl -e 'print 15314.6 + 16320.6 + 15813.6 + 16835.6 + 19252.1 + 14823.5 + 16577.1 + 15068.1 + 13631 + 18158.1 + 17889.6 + 16835.6 + 15314.6 + 15314.6 + 16320.6 + 17358.6 + 15563.1 + 18158.1 + 18158.1 + 16835.6 + 17889.6 + 16577.1 + 253294000 + 17889.6 + 19252.1 + 17623.1 + 18975.6 + 12266 + 19530.6 + 18158.1 + 18158.1 + 17096.1 + 17623.1 + 19530.6 + 19252.1 + 21536.1 + 18428.6 + 19811.1 + 17096.1 + 19811.1 + 18428.6 + 18975.6 + 18158.1 + 18975.6 + 17096.1 + 19530.6 + 18158.1 + 16577.1 + 18701.1 + 16577.1 + 19811.1 + 16066.1 + 18158.1 + 14340.5 + 17623.1 + 17358.6 + 18701.1 + 16577.1 + 16320.6 + 18975.6 + 19252.1 + 16320.6 + 13398.5 + 18975.6 + 15563.1 + 17889.6 + 16835.6 + 14102 + 11183.5 + 18158.1 + 18158.1 + 17623.1 + 17623.1 + 14581 + 16835.6 + 16066.1 + 17889.6 + 14340.5 + 17889.6 + 18158.1 + 17889.6 + 16066.1 + 18701.1 + 20664.6 + 20093.6 + 18158.1 + 21830.7 + 17623.1 + 20093.6 + 17358.6 + 19811.1 + 19530.6 + 19530.6 + 18975.6 + 18975.6 + 19530.6 + 19252.1 + 20953.1 + 20378.1 + 17889.6 + 17889.6 + 17358.6 + 19252.1 + 18975.6 + 18158.1 + 16835.6 + 18158.1 + 18975.6 + 14340.5 + 16835.6 + 15813.6 + 19811.1 + 17096.1 + 17889.6 + 14102 + 15813.6 + 19252.1 + 18158.1 + 16577.1 + 20093.6 + 20378.1'
255411071.499999

1 Like

the perl solution seems to work. does anyone know how to use awk for this?

Example:

$ echo ""|awk '{a=15314.6 + 16320.6 + 15813.6 + 16835.6 + 19252.1 + 14823.5 + 16577.1 + 15068.1 + 13631 + 18158.1 + 17889.6 + 16835.6 + 15314.6 + 15314.6 + 16320.6 + 17358.6 + 15563.1 + 18158.1 + 18158.1 + 16835.6 + 17889.6 + 16577.1 + 2.53294e+08 + 17889.6 + 19252.1 + 17623.1 + 18975.6 + 12266 + 19530.6 + 18158.1 + 18158.1 + 17096.1 + 17623.1 + 19530.6 + 19252.1 + 21536.1 + 18428.6 + 19811.1 + 17096.1 + 19811.1 + 18428.6 + 18975.6 + 18158.1 + 18975.6 + 17096.1 + 19530.6 + 18158.1 + 16577.1 + 18701.1 + 16577.1 + 19811.1 + 16066.1 + 18158.1 + 14340.5 + 17623.1 + 17358.6 + 18701.1 + 16577.1 + 16320.6 + 18975.6 + 19252.1 + 16320.6 + 13398.5 + 18975.6 + 15563.1 + 17889.6 + 16835.6 + 14102 + 11183.5 + 18158.1 + 18158.1 + 17623.1 + 17623.1 + 14581 + 16835.6 + 16066.1 + 17889.6 + 14340.5 + 17889.6 + 18158.1 + 17889.6 + 16066.1 + 18701.1 + 20664.6 + 20093.6 + 18158.1 + 21830.7 + 17623.1 + 20093.6 + 17358.6 + 19811.1 + 19530.6 + 19530.6 + 18975.6 + 18975.6 + 19530.6 + 19252.1 + 20953.1 + 20378.1 + 17889.6 + 17889.6 + 17358.6 + 19252.1 + 18975.6 + 18158.1 + 16835.6 + 18158.1 + 18975.6 + 14340.5 + 16835.6 + 15813.6 + 19811.1 + 17096.1 + 17889.6 + 14102 + 15813.6 + 19252.1 + 18158.1 + 16577.1 + 20093.6 + 20378.1; printf "%f\n", a}'

255411071.499999

awk...

echo '15314.6 16320.6 15813.6 16835.6 19252.1 14823.5 16577.1 15068.1 13631 18158.1 17889.6 16835.6 15314.6 15314.6 16320.6 17358.6 15563.1 18158.1 18158.1 16835.6 17889.6 16577.1 253294000 17889.6 19252.1 17623.1 18975.6 12266 19530.6 18158.1 18158.1 17096.1 17623.1 19530.6 19252.1 21536.1 18428.6 19811.1 17096.1 19811.1 18428.6 18975.6 18158.1 18975.6 17096.1 19530.6 18158.1 16577.1 18701.1 16577.1 19811.1 16066.1 18158.1 14340.5 17623.1 17358.6 18701.1 16577.1 16320.6 18975.6 19252.1 16320.6 13398.5 18975.6 15563.1 17889.6 16835.6 14102 11183.5 18158.1 18158.1 17623.1 17623.1 14581 16835.6 16066.1 17889.6 14340.5 17889.6 18158.1 17889.6 16066.1 18701.1 20664.6 20093.6 18158.1 21830.7 17623.1 20093.6 17358.6 19811.1 19530.6 19530.6 18975.6 18975.6 19530.6 19252.1 20953.1 20378.1 17889.6 17889.6 17358.6 19252.1 18975.6 18158.1 16835.6 18158.1 18975.6 14340.5 16835.6 15813.6 19811.1 17096.1 17889.6 14102 15813.6 19252.1 18158.1 16577.1 20093.6 20378.1' | tr ' ' '\n' | awk '{s+=$0}END{print s}'
1 Like

i beleive this is what i need. it gives the precise numbers. however, if put all those numbers in a variable, it doesn't work.

echo ""|awk "{a='${JUMBO}'; printf "%f\n", a}"

any ideas?

echo ""|awk "{a=${JUMBO}; printf \"%f\n\", a}"

---------- Post updated at 03:14 AM ---------- Previous update was at 01:14 AM ----------

or if you prefer this way:

$ echo "$JUMBO" |awk 'BEGIN{FS="\n";RS=" ";}{s+=$0}END{printf "%f\n", s}'
255411071.499999
1 Like