|
Home > Archive > Unix Shell > December 2007 > Sum of numbers in a file
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 |
Sum of numbers in a file
|
|
| Heiko@Heiko.Edu 2007-12-05, 1:24 pm |
| 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
| |
| Ed Morton 2007-12-05, 1:24 pm |
|
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.
| |
| Bill Marcum 2007-12-05, 1:24 pm |
| On 2007-12-05, Heiko@Heiko.Edu <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}'
| |
| chris.l.bryant@gmail.com 2007-12-05, 1:24 pm |
| On Dec 5, 8:43 am, "He...@Heiko.Edu" <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
| |
| Chris F.A. Johnson 2007-12-05, 1:24 pm |
| 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 <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
| |
|
| On Wed, 5 Dec 2007 13:56:17 -0500, "Chris F.A. Johnson" <cfajohnson@gmail.com> wrote:
>On 2007-12-05, Heiko@Heiko.Edu wrote:
>
>echo $(( $( tr '\012' '+' < FILE ) 0 ))
^--> What is this zero doing please?
Grant.
| |
| Wenhua Zhao 2007-12-05, 7:23 pm |
| On Dec 5, 1:06 pm, Grant <g_r_a_n...@dodo.com.au> wrote:
> On Wed, 5 Dec 2007 13:56:17 -0500, "Chris F.A. Johnson" <cfajohn...@gmail.com> wrote:
>
>
>
>
>
>
> ^--> What is this zero doing please?
For the last line, I guess.
| |
| Chris F.A. Johnson 2007-12-05, 7:23 pm |
| On 2007-12-05, Grant wrote:
>
>
> On Wed, 5 Dec 2007 13:56:17 -0500, "Chris F.A. Johnson" <cfajohnson@gmail.com> wrote:
>
> ^--> 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
| |
| Stephane Chazelas 2007-12-06, 1:36 am |
| On Wed, 5 Dec 2007 13:56:17 -0500, Chris F.A. Johnson wrote:
> On 2007-12-05, Heiko@Heiko.Edu wrote:
>
> 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
| |
| Michael Tosch 2007-12-06, 7:25 pm |
| chris.l.bryant@gmail.com wrote:
> On Dec 5, 8:43 am, "He...@Heiko.Edu" <He...@Heiko.Edu> wrote:
>
> 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
| |
| Heiko@Heiko.Edu 2007-12-07, 7:23 pm |
| On 06 Dec 2007 07:19:30 GMT, Stephane Chazelas
<stephane_chazelas@yahoo.fr> wrote:
>On Wed, 5 Dec 2007 13:56:17 -0500, Chris F.A. Johnson wrote:
>
>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
| |
| Loki Harfagr 2007-12-12, 7:32 am |
| Thu, 06 Dec 2007 21:33:36 +0100, Michael Tosch did catÂ_:
> Heiko@Heiko.Edu wrote:
> For very big numbers use this:
>
> awk '{print "x+=" $1+0} END {print "x"}' file | bc
Or, differently:
awk '1; END{ORS="\n";print 0}' ORS='+' file | bc
I use to use this form but I reckon it doesn't protect the
non numeric values like your script does :-)
Though I suppose if the data is broken at this place it's
probably better to fix the previous process first :-)
|
|
|
|
|