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

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}’

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: