Note that there are some explanatory texts on larger screens.

plurals
  1. POShell command to sum integers, one per line?
    text
    copied!<p>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.</p> <p>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 <code>sed</code> 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 <code>expr</code> in the past, but unless it runs in <code>RPN mode</code> I don't think it's going to cope with this (and even then it would be tricky).</p> <p>What am I missing? Given that there are probably several ways to achieve this, I will be happy to read (and <code>upvote</code>) any approach that works, even if someone else has already posted a different solution that does the job.</p> <p>Related question: <a href="https://stackoverflow.com/questions/295781/shortest-command-to-calculate-the-sum-of-a-column-of-output-on-unix">Shortest command to calculate the sum of a column of output on Unix? </a> (credits <a href="https://stackoverflow.com/questions/450799/linux-command-to-sum-integers-one-per-line#451313">@Andrew</a>)</p> <hr> <p><strong>Update</strong>: Wow, as expected there are some nice answers here. Looks like I will definitely have to give <code>awk</code> deeper inspection as a <code>command-line tool</code> in general!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload