shell script add each row value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script add each row value
# 1  
Old 01-27-2009
shell script add each row value

Hi,

I wrote below small script which will add up each row value from a file and print the total, but it is not.plz let me know what is wrong.script hangs and not displaying anything.
-----------------
while read line ; do
sum=0;
sum=$sum+$line;
done
echo $sum;
exit 0;
------------------
file has below rows:
37.17
65.53
106.14
45.12
# 2  
Old 01-27-2009
The first thing I notice offhand is that you zero out the sum inside the while loop for every line.

But also, you don't display how you are getting the contents of the file to be read in the while loop. Can you show what you did there. If nothing, than that is the reason the script is "hanging". It's reading stdin which means it's awaiting your input.

Then there is the problem that you are working with floating point numbers. Does your shell handle that? Mine doesn't
# 3  
Old 01-27-2009
shell script add each row value

Hi,

I have modified the script:

FILE=/tmp/cc
SUM=0;
cat $$FILE | while reaad line; do
SUM=$SUM+$LINE;
done
echo $SUM;
exit 0;

but when I runn the script, it says "./cc.sh[6]: 0+38.08: not found"
# 4  
Old 01-27-2009
You have solved a few problems. You've got the file contents going into the While loop so that's good, (I'm assuming the double $$ before FILE is just a typo, if not get rid of one '$' there).

You're not zeroing out the sum, IN the while loop. Also good.

But that leaves us with the fact that your still dealing with floating point numbers, not integers.

Do a 'man' on your shell and see if it supports 'typeset -E' or 'typeset -F'. If so, try using:

Code:
typeset -F SUM
typeset -F LINE

before you set SUM=0
# 5  
Old 01-27-2009
the easiest way:
Code:
#!/bin/ksh
echo $(sed -e 's/$/+/' /tmp/cc) 0 | bc

# 6  
Old 01-27-2009
shell script add each row value

Hi,

I tried this, got it while search.

awk '{sum += $1} END {printf "%.2f\n", sum}' file..it gave the ans, but do not know if it correct..

vgersh99, can you explain the command?is this a oneline command for adding the values in rows in file?

rwuerth, I put the typeset in script,

But it gaves the same error, but decimal numbers are missing in error.
# 7  
Old 01-27-2009
You will have to add it up manually to verify the correct answer, but if you're just using the numbers you gave us above as a test file, the answer would be 253.96.

If using typeset -F or typeset -E isn't working it's probably not supported.

vgresh99's response is perfect actually. You could wrap that up in a command substition to put the value into a variable for use elsewhere if needed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read one row at time using shell script?

Hello Team, I need information on how to read one row at time using shell script. For example i have below data. service-description servername warning critical mountpoint disk-usage-tmp generic-service test1 80 90 /tmp disk-usage-var ... (6 Replies)
Discussion started by: ghpradeep
6 Replies

2. Shell Programming and Scripting

Add Row from First Row (Split Row)

HI Guys, I have Below Input :- RepigA_hteis522 ReptCfiEtrBsCll_aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 RepigA ReptCfiEtrBsCll hteis522 aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 Split Data in two first row... (2 Replies)
Discussion started by: pareshkp
2 Replies

3. Shell Programming and Scripting

Change col to row using shell script..Very Complex

Hi guys I have file A with Below Data ABC123 X1 X2 X3 ABC123 Y1 Y33 Y4 ABC123 Z1 ZS2 ZL3 ABC234 P1 PP3 PP9 ABC234 Q1 ABC234 R1 P09 PO332 PO331 OKI12 .. .. .. Now I want file B as below ABC123 X1 X2 X3;Y1 Y33 Y4;Z1 ZS2 ZL3 ABC234 P1 PP3 PP9;Q1;R1 P09 PO332 PO331 OKI12... (1 Reply)
Discussion started by: asavaliya
1 Replies

4. Shell Programming and Scripting

How to arrange xml tags in single row using shell script?

I want to put one xml record in one row and so on... sample two records are here. <?xml version="1.0"?> <Object> <Header> <XCOMVers>V1.0</XCOMVers> <REPORT>XXXXX</REPORT> <CODE>002</CODE> </Header> <IssueCard> <Record> <L>CAR SYSTEM -SSSSS -</L> ... (1 Reply)
Discussion started by: sene_geet
1 Replies

5. Shell Programming and Scripting

Want to remove the last characters from each row of csv using shell script

Hi, I've a csv file seperated by '|' from which I'm trying to remove the excess '|' characters more than the existing fields. My CSV looks like as below. HRLOAD|Service|AddChange|EN PERSONID|STATUS|LASTNAME|FIRSTNAME|ITDCLIENTUSERID|ADDRESSLINE1 10000001|ACTIVE|Testazar1|Testore1|20041|||... (24 Replies)
Discussion started by: rajak.net
24 Replies

6. UNIX for Dummies Questions & Answers

Shell Script: Traverse Database Table Row by Row

Hello Everyone, My issue is that I want to traverse a database table row by row and do some action on the value retrieved in each row. I have gone through a lot of shell script questions/posts. I could find row by row traversal of a file but not a database table. Please help. Thanks &... (5 Replies)
Discussion started by: ahsan.asghar
5 Replies

7. Shell Programming and Scripting

Spliting a row to multiple rows using shell script

Please advice script for changing from A to B I'd like to Split a row to multiple rows with 4 columns using shell script. The data of the file is in one row as below that is 28Mbyte size. A> cto10001 0000000 201010 10:52:13 cto10001 0000000 201011 10:52:13 cto10001 0000000 201011 10:52:13... (2 Replies)
Discussion started by: ianpapa
2 Replies

8. Shell Programming and Scripting

Script shell on line and row

Hello, I want to make a script to do this : the file is with line : TOTO TEST1 TOTO TEST2 TITI TEST1 TITI TEST2 TITI TEST3 i want he become : TOTO TEST1 TEST2 TITI TEST1 TEST2 TEST3 (3 Replies)
Discussion started by: safsound
3 Replies

9. Shell Programming and Scripting

How to do row comparison in shell script

Hi, I need help on doing the below thing in shell script. I have a file with millions of rows called "abc.txt". i have another file with millions of rows called "xyz.txt". I would like to do the below operation. Open the abc.txt, read the first line, do some operations on the column... (2 Replies)
Discussion started by: informsrini
2 Replies

10. Shell Programming and Scripting

Add value of each row when each row has different data ype

Guys -- I am noob and I am really strugging on this one. I cant even write the pseudo code for it. I have a file that spits values in individual rows as such: file: user date type size joe 2005-25-09 log 1 joe 2005-25-09 snd 10 joe 2005-25-09 rcd 12 joe 2005-24-09 log 2 joe... (1 Reply)
Discussion started by: made2last
1 Replies
Login or Register to Ask a Question