How can I quickly sum all numbers in a file?
You can use awk:
awk '{ sum += $1 } END { print sum }' file
None of the solution thus far use paste
. Here's one:
paste -sd+ filename | bc
As an example, calculate Σn where 1<=n<=100000:
$ seq 100000 | paste -sd+ | bc -l
5000050000
(For the curious, seq n
would print a sequence of numbers from 1
to n
given a positive number n
.)