LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-29-2007, 04:31 AM   #1
mystical dervish
LQ Newbie
 
Registered: May 2007
Posts: 25

Rep: Reputation: 15
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 ....
 
Old 05-29-2007, 04:50 AM   #2
tread
LQ Newbie
 
Registered: Feb 2007
Posts: 29

Rep: Reputation: 15
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.
 
Old 05-30-2007, 10:14 AM   #3
mystical dervish
LQ Newbie
 
Registered: May 2007
Posts: 25

Original Poster
Rep: Reputation: 15
Yes exactly
but I tried to delete the blank line
Code:
sed s'/^$//g' f1.txt > outf.txt
but it does nothing !
 
Old 05-30-2007, 10:31 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
let's have some awk then
Code:
awk '{count+=$1}END{print "total: " count}' file
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 09:10 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
linux 9 and java script error - premature end of script header sibil Linux - Newbie 0 01-06-2004 04:21 PM
summation of tainted module? toastermaker General 5 12-16-2003 08:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration