Problems with sum operation


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Problems with sum operation
# 1  
Old 05-15-2006
Problems with sum operation

i wrote this code..

#!/bin/sh
sum=0
for i in `cat numbers.txt |cut -f1`
do
sum=expr $sum + $i
done
echo $sum


I want to read the numbers in the file numbers.txt. and find sum of them. But this code only writes the numbers(without making sum) that it reads. Help me..
# 2  
Old 05-15-2006
Try a bit of awk

burakkilic,

I normally use awk for this kind of problem - it works effectively

awk '{tot+=$1} END{print tot}' numbers.txt

Alternatively, change your script as shown:

#!/bin/sh
sum=0
for i in `cat numbers.txt |cut -f1`
do
sum=`expr $sum + $i` # Add back quotes around the expr
done
echo $sum


Steve
# 3  
Old 05-15-2006
Thank you very much!.. The code is working properlySmilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Do replace operation and awk to sum multiple columns if another column has duplicate values

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (12 Replies)
Discussion started by: as7951
12 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Sum operation

I have file input w34 AG1 2 184 w35 AG1 6 552 w35 BG1 12 0 w35 CD1 7 0 w36 CG1 4 0 my output should be w34 AG1 2 184 0.991 w35 AG1 6 552 0.991 w35 BG1 12 0 1.000 w35 CD1 7 0... (3 Replies)
Discussion started by: radius
3 Replies

3. Shell Programming and Scripting

If then else - Retry operation

I need to read a file line by line, then depending on the contents of each line, type in a code that will get written to an array. The problem I have is when I ask the user to confirm the input code, if it is wrong, how do i Return to ask again? Any thing I try increments the file to the next... (6 Replies)
Discussion started by: kcpoole
6 Replies

4. Shell Programming and Scripting

Column operation : cosne and sine operation

I have a txt file with several columns and i want to peform an operation on two columns and output it to a new txt file . file.txt 900.00000 1 1 1 500.00000 500.00000 100000.000 4 4 1.45257346E-07 899.10834 ... (4 Replies)
Discussion started by: shashi792
4 Replies

5. Shell Programming and Scripting

Print sum and relative value of the sum

Hi i data looks like this: student 1 Subject1 45 55 Subject2 44 55 Subject3 33 44 // student 2 Subject1 45 55 Subject2 44 55 Subject3 33 44 i would like to sum $2, $3 (marks) and divide each entry in $2 and $3 with their respective sums and print for each student as $4 and... (2 Replies)
Discussion started by: saint2006
2 Replies

6. Shell Programming and Scripting

sed OR-Operation

Hello, I'm trying to get the configuration-IP-Addresses from Cisco-configurations on the Routers they are defined as a Loopback0-interface like this: interface Loopback0 ip address 172.23.19.249 255.255.255.255 On the Switches they are defined as a VLAN 80 interface like this ... (4 Replies)
Discussion started by: Sally[-_-]
4 Replies

7. Shell Programming and Scripting

Array operation

Hi, I would like ask for you help for coding array operation. array= ( a b c d e f ) I would like to remove entry "d" from my array and import the remaining entries back to the array. Thanks. (3 Replies)
Discussion started by: phamp008
3 Replies

8. Shell Programming and Scripting

Help with arithmetic operation

I am using egrep to extract numbers from a file and storing them as variables in a script. But I am not able to do any arithmetic operations on the variables using "expr" because it stores them as char and not integers. Here is my code and the error I get. Any help will be appreciated. #!/bin/sh... (3 Replies)
Discussion started by: emjayshaikh
3 Replies

9. Shell Programming and Scripting

split operation

Hello, How to undo split operation ? (1 Reply)
Discussion started by: scotty_123
1 Replies

10. Shell Programming and Scripting

string operation

Hi all, Here is my situation. I have a text file TXT_FILE like this: john 123456 jack 94589 kelvin 94595 mary 88585 I want to read the first word in each line ( the name ) and assign to a string variable ( EX_LIST ) in my script so that I can use later as this commandfor i in... (6 Replies)
Discussion started by: fongthai
6 Replies
Login or Register to Ask a Question