Sum of numbers in a file
am 05.12.2007 15:43:06 von heiko
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
Re: Sum of numbers in a file
am 05.12.2007 15:57:53 von Ed Morton
On 12/5/2007 8:43 AM, Heiko@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.
Re: Sum of numbers in a file
am 05.12.2007 16:07:41 von Bill Marcum
On 2007-12-05, Heiko@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}'
Re: Sum of numbers in a file
am 05.12.2007 18:21:40 von chris.l.bryant
On Dec 5, 8:43 am, "He...@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
Re: Sum of numbers in a file
am 05.12.2007 19:56:17 von cfajohnson
On 2007-12-05, Heiko@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
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
Re: Sum of numbers in a file
am 05.12.2007 22:06:16 von G_r_a_n_t_
On Wed, 5 Dec 2007 13:56:17 -0500, "Chris F.A. Johnson" wrote:
>On 2007-12-05, Heiko@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.
Re: Sum of numbers in a file
am 05.12.2007 23:54:07 von Wenhua Zhao
On Dec 5, 1:06 pm, Grant wrote:
> On Wed, 5 Dec 2007 13:56:17 -0500, "Chris F.A. Johnson" wrote:
>
> >On 2007-12-05, He...@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.
Re: Sum of numbers in a file
am 06.12.2007 00:36:28 von cfajohnson
On 2007-12-05, Grant wrote:
>
>
> On Wed, 5 Dec 2007 13:56:17 -0500, "Chris F.A. Johnson" wrote:
>
>>On 2007-12-05, Heiko@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
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
Re: Sum of numbers in a file
am 06.12.2007 08:19:30 von Stephane CHAZELAS
On Wed, 5 Dec 2007 13:56:17 -0500, Chris F.A. Johnson wrote:
> On 2007-12-05, Heiko@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
Re: Sum of numbers in a file
am 06.12.2007 21:33:19 von Michael Tosch
chris.l.bryant@gmail.com wrote:
> On Dec 5, 8:43 am, "He...@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 @ hp : com
Re: Sum of numbers in a file
am 06.12.2007 21:33:36 von Michael Tosch
Heiko@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 @ hp : com
Re: Sum of numbers in a file
am 07.12.2007 23:59:55 von heiko
On 06 Dec 2007 07:19:30 GMT, Stephane Chazelas
wrote:
>On Wed, 5 Dec 2007 13:56:17 -0500, Chris F.A. Johnson wrote:
>> On 2007-12-05, Heiko@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