Adding Numbers in a line from a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Adding Numbers in a line from a file
# 1  
Old 04-18-2009
Data Adding Numbers in a line from a file

Hello everyone !

Ive searched everywhere and still havnt found enough information to help me overcome (what seems like) a small problem

I have created a temporary file in which i store numbers which a seperated by a space, eg)

Code:
5 10 46
23 866 392

i wish to take the numbers for each line and add them together to find the sum for each line.

I am confused at how to specify the numbers too add ie) if i do this:
while read line
do
m=0
sum=0

while (( m++ < ${#line} ))
do
num=$(expr substr "$line" $m 1)
(( sum=sum+num))
done
echo $sum
done < fileNums

But this looks at each character, where as i want to look at each field seperated by a space.

I realise this can be specified using cut, with a deliminator to seperate each field - but I dont know how to iterate through each field in a line, especially since I dont know how many fields will be in each line.

Im using bash, and am trying to do this without using awk or sed. Using expr is the way I want to go.
Any help would be appreciated! I have been stuck for days!! Thank you.

Last edited by Yogesh Sawant; 04-20-2009 at 04:35 AM.. Reason: added code tags
# 2  
Old 04-18-2009
Why wouldn't you want to use awk or sed? And if you would use one of these tools how would the solution look like?

Sorry, but this sounds like homework, which is forbidden by the rules.

If it is not homework (and the awk&sed-restriction is simply because it was said so) please tell us more about the task and why you wouldn't want to use awk or sed.

Even if its homework i would be willing to give some leeway in this and help you with a hint (certainly not a solution), but to be eligible you will have to answer the second question, how would you solve it with awk or sed.

bakunin
# 3  
Old 04-20-2009
Substitute the space(s) with the + symbol, and do an expr on it ...

Got it ?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding line in a file using info from previous line

I have a shell script that looks something like the following: mysql -uroot db1 < db1.sql mysql -uroot db2 < db2.sql mysql -uroot db3 < db3.sql mysql -uroot db4 < db4.sql .... different db names in more than 160 lines. I want to run this script with nohup and have a status later. So,... (6 Replies)
Discussion started by: MKH
6 Replies

2. Shell Programming and Scripting

Adding new line to file

Hi everyone, currently I writing a script for comparing 2 variable in 2 line then output the line with equal value to new file. However, the new file only contain last line only, the earlier line was delete. I do google my problem but still not find the way out. Sorry for my English. Thank you... (10 Replies)
Discussion started by: lazy_bear
10 Replies

3. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

4. UNIX for Dummies Questions & Answers

Adding a column to a text file with row numbers

Hi, I would like to add a new column containing the row numbers to a text file. How do I go about doing that? Thanks! Example input: A X B Y C D Output: A X 1 B Y 2 C D 3 (5 Replies)
Discussion started by: evelibertine
5 Replies

5. Shell Programming and Scripting

Adding tab/new line at the end of each line of a file

Hello Everyone, I need a help from experts of this community regarding one of the issue that I am facing with shell scripting. My requirement is to append char's at the end of each line of a file. The char that will be appended is variable and will be passed through command line. The... (20 Replies)
Discussion started by: Sourav Das
20 Replies

6. Shell Programming and Scripting

Adding patterns with increasing numbers at the end of every line

Hi All, I have a file which has some Linux commands in every line like more 456.dat more 3232.dat more 433.dat I want to add texts like this at the end of every line: more 456.dat > 1.txt more 3232.dat > 2.txt more 433.dat > 3.txt So, that the result of more goes into 1.txt... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

7. Shell Programming and Scripting

Assign Line Numbers to each line of the file

Hi! I'm trying to assign line numbers to each line of the file for example consider the following.. The contents of the input file are hello how are you? I'm fine. How about you? I'm trying to get the following output.. 1 hello how are you? 2 I'm fine. 3 How about you? ... (8 Replies)
Discussion started by: abk07
8 Replies

8. Shell Programming and Scripting

adding a line to file

i am writing a script which will let user to input a line. i m not sure how do i add this line to the end of a txt file ? (8 Replies)
Discussion started by: 76455
8 Replies

9. Shell Programming and Scripting

Adding a line to a file

Hi guys, How to add a line before a specific line (identified with the starting work ex: xxx) of a file and write it back to the same file? Thanks (12 Replies)
Discussion started by: mwrg
12 Replies

10. Shell Programming and Scripting

adding a line to a file

I want to add a line at the beginning and at the end of a file.. e.g. echo "at the beginning.." > tmp_file && cat file >> tmp_file && echo "last line" >> tmp_file && mv tmp_file file is there a nice way for doing that?? Thx (2 Replies)
Discussion started by: andy2000
2 Replies
Login or Register to Ask a Question