Sum of numbers in a file

Hello all,

I have a file in the format like this:

2
85
982
1067
13924
25892

I would like to be able to sum these.

I have read the man pages, google groups, O'Reilly texts, etc, and
have come up empty.

Any help is much appreciated... and no, this is not a homework
assignment - this is a project for my job.

Regards,

Heiko
heiko [ Mi, 05 Dezember 2007 15:43 ] [ ID #1885919 ]

Re: Sum of numbers in a file

On 12/5/2007 8:43 AM, Heiko [at] Heiko.Edu wrote:
> Hello all,
>
> I have a file in the format like this:
>
> 2
> 85
> 982
> 1067
> 13924
> 25892
>
> I would like to be able to sum these.
>
> I have read the man pages, google groups, O'Reilly texts, etc, and
> have come up empty.
>
> Any help is much appreciated... and no, this is not a homework
> assignment - this is a project for my job.
>
> Regards,
>
> Heiko

awk '{s+=$0}END{print s}' file

Ed.
Ed Morton [ Mi, 05 Dezember 2007 15:57 ] [ ID #1885920 ]

Re: Sum of numbers in a file

On 2007-12-05, Heiko [at] Heiko.Edu <Heiko [at] Heiko.Edu> wrote:
>
>
> Hello all,
>
> I have a file in the format like this:
>
> 2
> 85
> 982
> 1067
> 13924
> 25892
>
> I would like to be able to sum these.
>
> I have read the man pages, google groups, O'Reilly texts, etc, and
> have come up empty.
>
awk '{sum += $1} END{print sum}'
Bill Marcum [ Mi, 05 Dezember 2007 16:07 ] [ ID #1885921 ]

Re: Sum of numbers in a file

On Dec 5, 8:43 am, "He... [at] Heiko.Edu" <He... [at] Heiko.Edu> wrote:
> Hello all,
>
> I have a file in the format like this:
>
> 2
> 85
> 982
> 1067
> 13924
> 25892
>
> I would like to be able to sum these.
>
> I have read the man pages, google groups, O'Reilly texts, etc, and
> have come up empty.
>
> Any help is much appreciated... and no, this is not a homework
> assignment - this is a project for my job.
>
> Regards,
>
> Heiko

you can either do it with 'bc' or you may need to write a script like
so

------------------------------------------------------------ ------------------
#!/bin/ksh

INPUT_FILE=$1
set -A SUM `cat $INPUT_FILE`
index=1
elements=`wc -l $INPUT_FILE|awk '{print $1}'`

NEW_SUM=${SUM[0]}
while [[ $index -lt $elements ]]; do
NEW_SUM=`expr $NEW_SUM + ${SUM[$index]}`
index=`expr $index + 1`
done

echo $NEW_SUM
------------------------------------------------------------ ------------------

where INPUT_FILE is the elements you want to sum. However this will
only work for 1023 elements.

HTH
chris.l.bryant [ Mi, 05 Dezember 2007 18:21 ] [ ID #1885925 ]

Re: Sum of numbers in a file

On 2007-12-05, Heiko [at] Heiko.Edu wrote:
>
> I have a file in the format like this:
>
> 2
> 85
> 982
> 1067
> 13924
> 25892
>
> I would like to be able to sum these.

echo $(( $( tr '\012' '+' < FILE ) 0 ))

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
cfajohnson [ Mi, 05 Dezember 2007 19:56 ] [ ID #1885931 ]

Re: Sum of numbers in a file

On Wed, 5 Dec 2007 13:56:17 -0500, "Chris F.A. Johnson" <cfajohnson [at] gmail.com> wrote:

>On 2007-12-05, Heiko [at] Heiko.Edu wrote:
>>
>> I have a file in the format like this:
>>
>> 2
>> 85
>> 982
>> 1067
>> 13924
>> 25892
>>
>> I would like to be able to sum these.
>
>echo $(( $( tr '\012' '+' < FILE ) 0 ))
^--> What is this zero doing please?

Grant.
G_r_a_n_t_ [ Mi, 05 Dezember 2007 22:06 ] [ ID #1885935 ]

Re: Sum of numbers in a file

On Dec 5, 1:06 pm, Grant <g_r_a_n... [at] dodo.com.au> wrote:
> On Wed, 5 Dec 2007 13:56:17 -0500, "Chris F.A. Johnson" <cfajohn... [at] gmail.com> wrote:
>
> >On 2007-12-05, He... [at] Heiko.Edu wrote:
>
> >> I have a file in the format like this:
>
> >> 2
> >> 85
> >> 982
> >> 1067
> >> 13924
> >> 25892
>
> >> I would like to be able to sum these.
>
> >echo $(( $( tr '\012' '+' < FILE ) 0 ))
>
> ^--> What is this zero doing please?
For the last line, I guess.
Wenhua Zhao [ Mi, 05 Dezember 2007 23:54 ] [ ID #1885937 ]

Re: Sum of numbers in a file

On 2007-12-05, Grant wrote:
>
>
> On Wed, 5 Dec 2007 13:56:17 -0500, "Chris F.A. Johnson" <cfajohnson [at] gmail.com> wrote:
>
>>On 2007-12-05, Heiko [at] Heiko.Edu wrote:
>>>
>>> I have a file in the format like this:
>>>
>>> 2
>>> 85
>>> 982
>>> 1067
>>> 13924
>>> 25892
>>>
>>> I would like to be able to sum these.
>>
>>echo $(( $( tr '\012' '+' < FILE ) 0 ))
> ^--> What is this zero doing please?

Making it a valid expression. Look at the output of tr.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
cfajohnson [ Do, 06 Dezember 2007 00:36 ] [ ID #1886753 ]

Re: Sum of numbers in a file

On Wed, 5 Dec 2007 13:56:17 -0500, Chris F.A. Johnson wrote:
> On 2007-12-05, Heiko [at] Heiko.Edu wrote:
>>
>> I have a file in the format like this:
>>
>> 2
>> 85
>> 982
>> 1067
>> 13924
>> 25892
>>
>> I would like to be able to sum these.
>
> echo $(( $( tr '\012' '+' < FILE ) 0 ))

It should ne noted, that contrary to the awk solution, that one
doesn't scale very well as it stores the whole addition
expression in memory before computing it. While the awk one does
the addition while reading the file. So for big files, it will
waste a lot of memory. The awk solution will also behave better
if there are lines that don't look like numbers.

In case files may be empty, one may prefer:

awk '
{sum += $0}
END {print sum + 0}' < FILE

To be sure "0" is output in that case.

--
Stephane
Stephane CHAZELAS [ Do, 06 Dezember 2007 08:19 ] [ ID #1886756 ]

Re: Sum of numbers in a file

chris.l.bryant [at] gmail.com wrote:
> On Dec 5, 8:43 am, "He... [at] Heiko.Edu" <He... [at] Heiko.Edu> wrote:
>> Hello all,
>>
>> I have a file in the format like this:
>>
>> 2
>> 85
>> 982
>> 1067
>> 13924
>> 25892
>>
>> I would like to be able to sum these.
>>
>> I have read the man pages, google groups, O'Reilly texts, etc, and
>> have come up empty.
>>
>> Any help is much appreciated... and no, this is not a homework
>> assignment - this is a project for my job.
>>
>> Regards,
>>
>> Heiko
>
> you can either do it with 'bc' or you may need to write a script like
> so
>
> ------------------------------------------------------------ ------------------
> #!/bin/ksh
>
> INPUT_FILE=$1
> set -A SUM `cat $INPUT_FILE`
> index=1
> elements=`wc -l $INPUT_FILE|awk '{print $1}'`
>
> NEW_SUM=${SUM[0]}
> while [[ $index -lt $elements ]]; do
> NEW_SUM=`expr $NEW_SUM + ${SUM[$index]}`
> index=`expr $index + 1`
> done
>

Too complicated!

#!/bin/ksh
sum=0
while read element
do
sum=$((sum+$element))
done < file
echo $sum

--
Michael Tosch [at] hp : com
Michael Tosch [ Do, 06 Dezember 2007 21:33 ] [ ID #1886792 ]

Re: Sum of numbers in a file

Heiko [at] Heiko.Edu wrote:
> Hello all,
>
> I have a file in the format like this:
>
> 2
> 85
> 982
> 1067
> 13924
> 25892
>
> I would like to be able to sum these.
>
> I have read the man pages, google groups, O'Reilly texts, etc, and
> have come up empty.
>
> Any help is much appreciated... and no, this is not a homework
> assignment - this is a project for my job.
>
> Regards,
>
> Heiko
>
>

For very big numbers use this:

awk '{print "x+=" $1+0} END {print "x"}' file | bc


--
Michael Tosch [at] hp : com
Michael Tosch [ Do, 06 Dezember 2007 21:33 ] [ ID #1886793 ]

Re: Sum of numbers in a file

On 06 Dec 2007 07:19:30 GMT, Stephane Chazelas
<stephane_chazelas [at] yahoo.fr> wrote:

>On Wed, 5 Dec 2007 13:56:17 -0500, Chris F.A. Johnson wrote:
>> On 2007-12-05, Heiko [at] Heiko.Edu wrote:
>>>
>>> I have a file in the format like this:
>>>
>>> 2
>>> 85
>>> 982
>>> 1067
>>> 13924
>>> 25892
>>>
>>> I would like to be able to sum these.
>>
>> echo $(( $( tr '\012' '+' < FILE ) 0 ))
>
>It should ne noted, that contrary to the awk solution, that one
>doesn't scale very well as it stores the whole addition
>expression in memory before computing it. While the awk one does
>the addition while reading the file. So for big files, it will
>waste a lot of memory. The awk solution will also behave better
>if there are lines that don't look like numbers.
>
>In case files may be empty, one may prefer:
>
>awk '
> {sum += $0}
> END {print sum + 0}' < FILE
>
>To be sure "0" is output in that case.

Thanks to all for their suggestions - works great.

-Heiko
heiko [ Fr, 07 Dezember 2007 23:59 ] [ ID #1887723 ]
Linux » comp.unix.shell » Sum of numbers in a file

Vorheriges Thema: Re: modifing a path (string)
Nächstes Thema: working with paths