LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   summation script (https://www.linuxquestions.org/questions/programming-9/summation-script-557389/)

mystical dervish 05-29-2007 04:31 AM

summation script
 
Hello,

I have a very long list of Numbers like

116159
64625
55525
52981
40661
39653
39550
32807
32483
29250
28808
.
.
.
5
3
I wanna have the total sum , in fact I tried this code
#!/bin/sh


a=0
while read line

do
a=`expr $a + $line`;
done < list.txt
echo "Final Sum is: $a";

it gives me error like
expr:Syntax error
but it results the sum out , how come ? could someone help because I don't know if the result is correct or not ....

tread 05-29-2007 04:50 AM

There must be a blank line at the end of the file. The last expr fails - try using "sh -x your_script" for debugging and a smaller input file - e.g.
Code:

1
2
3

Apart from that, looks fine.

mystical dervish 05-30-2007 10:14 AM

Yes exactly
but I tried to delete the blank line
Code:

sed s'/^$//g' f1.txt > outf.txt
but it does nothing !

ghostdog74 05-30-2007 10:31 AM

let's have some awk then :)
Code:

awk '{count+=$1}END{print "total: " count}' file


All times are GMT -5. The time now is 11:53 AM.