Andrzej Doyle Andrzej Doyle - 2 years ago 102
Bash Question

Shell command to sum integers, one per line?

I am looking for a command that will accept as input multiple lines of text, each line containing a single integer, and output the sum of these integers.

As a bit of background, I have a log file which includes timing measurements, so through grepping for the relevant lines, and a bit of

reformatting I can list all of the timings in that file. I'd like to work out the total however, and my mind has gone blank as to any command I can pipe this intermediate output to in order to do the final sum. I've always used
in the past, but unless it runs in
RPN mode
I don't think it's going to cope with this (and even then it would be tricky).

What am I missing? Given that there are probably several ways to achieve this, I will be happy to read (and
) any approach that works, even if someone else has already posted a different solution that does the job.

Related question: Shortest command to calculate the sum of a column of output on Unix? (credits @Andrew)

Update: Wow, as expected there are some nice answers here. Looks like I will definitely have to give
deeper inspection as a
command-line tool
in general!

Answer Source

Bit of awk should do it?

awk '{s+=$1} END {print s}' mydatafile

Note: some versions of awk have some odd behaviours if you are going to be adding anything exceeding 2^31 (2147483647) - see comments for more background. One suggestion is to use printf rather than print awk '{s+=$1} END {printf "%.0f", s}' mydatafile

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download