Unix Programming - Addition tools in Bourne

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > February 2006 > Addition tools in Bourne





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Addition tools in Bourne
Adam

2006-02-17, 10:40 pm

Hi,
In a shell, Bourne in my case, what tools would I use, can how would I
use them to add up a set of numbers in a file.
For example if I had a file holding...

filename
------------
10
15
7
2

How would I derive the total of those numbers, 34?

Any help what-so-ever appreciated.

Thanks
Adam.

Chris F.A. Johnson

2006-02-17, 10:40 pm

On 2006-02-15, Adam wrote:
> Hi,
> In a shell, Bourne in my case, what tools would I use, can how would I
> use them to add up a set of numbers in a file.
> For example if I had a file holding...
>
> filename
> ------------
> 10
> 15
> 7
> 2
>
> How would I derive the total of those numbers, 34?


See my answer in comp.unix.questions. (The first solution is POSIX
rather than Bourne.)

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Jim Cochrane

2006-02-17, 10:40 pm

In article <1140020571.771822.162140@z14g2000cwz.googlegroups.com>, Adam wrote:
> Hi,
> In a shell, Bourne in my case, what tools would I use, can how would I
> use them to add up a set of numbers in a file.
> For example if I had a file holding...
>
> filename
> ------------
> 10
> 15
> 7
> 2
>
> How would I derive the total of those numbers, 34?


There are many ways to do this - here is a pretty short/easy one:

awk '{sum += $1} END {print sum}' file


--
Jim Cochrane; jtc@dimensional.com
[When responding by email, include the term non-spam in the subject line to
get through my spam filter.]
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com