Archive for October, 2010

Quick way to sum every numbers in a file written line by line

October 1, 2010

If you have a file full of numbers written line by line, you can sum every line to get the total.

With a file like this:

34

9

6

91

23

you will get:

163

And the shell command to get this done is –

( sed ‘s/^/b+=/’  your-file ; echo b ) | bc

Or,

cat your file | awk ‘{ sum += $0 } END {print sum}’