Sum of numbers in row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum of numbers in row
# 1  
Old 12-19-2016
Sum of numbers in row

Need help in coding:
File with several rows incl. numbers like
Code:
1 2 3 4
5 6 7 8
...

How can i build the sum of each row seperately?
Code:
10
26
...

Thx for help.



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 12-19-2016 at 04:54 PM.. Reason: Added CODE tags.
# 2  
Old 12-19-2016
What have you tried?
How do you think you would approach this?
# 3  
Old 12-19-2016
Unix scripting is not my daily business, therefor i cannot answer you seriously.
I think that array will be the right way. I have found a bash script. Unfortunately it includes "seq" which is not installed in my environment..

---------- Post updated at 01:57 PM ---------- Previous update was at 01:52 PM ----------

Code:
#!/bin/bash
while read line; do
a=($line)
last=$((${#a[*]}-1))
sum=${a[0]}
echo -n "$sum "
for i in $(seq 1 $last); do         <-- this line does not work!
sum=$((sum+${a[$i]}))
echo -n "+ ${a[$i]} "
done
echo "= $sum"
done < input.txt



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 12-19-2016 at 04:36 PM.. Reason: Added CODE tags.
# 4  
Old 12-19-2016
Hello smitty11,

Could you please try following and let me know if this helps you.
Code:
awk '{for(i=1;i<=NF;i++){NUM=NUM?NUM+$i:$i};$(NF+1)=NUM;NUM=""} 1'   Input_file

Output will be as follows.
Code:
1 2 3 4 10
5 6 7 8 26

Thanks,
R. Singh
# 5  
Old 12-19-2016
* bash
Code:
#!/bin/bash
while read line; do
a=($line)
sum=0
for i in "${a[@]}"; do
sum=$((sum+$i))
done
echo "= $sum"
done <input.txt

* awk
Code:
awk '{sum=0; for(i=1; i<=NF; i++) sum += $i; print sum}'

* Perl
Code:
perl -Mstrict -MList::Util=sum -wanE 'say sum(@F)'

# 6  
Old 12-19-2016
Thx. Come back to you tomorrow...

---------- Post updated at 02:47 PM ---------- Previous update was at 02:38 PM ----------

By the way... how can i add up numbers in a column which are seperated by a blank line or character? Each result should be written in a outfile...
Code:
1
2
3
4
<blank line or character>
5
6
7
8

...
Result in outfile:
Code:
10
26

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 12-19-2016 at 04:55 PM.. Reason: Added CODE tags.
# 7  
Old 12-19-2016
Code:
1
2
3
4
<blank line or character>
5
6
7
8

Are they always the same character or different one each time? More than one character?

Last edited by Aia; 12-19-2016 at 05:08 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sum even numbers from 1 to 100

I need help with this assignment. I'm very new to using UNIX/LINUX, and my only previous experience with programing anything is using python. We are writing scripts using vim, and this one I'm stumped on. "Write a shell script that finds and display the sum of even positive integers from 0 to... (5 Replies)
Discussion started by: Nastybutler
5 Replies

2. Shell Programming and Scripting

Add sum of columns and max as new row

Hi, I am a new bie i need some help with respect to shell onliner; I have data in following format Name FromDate UntilDate Active Changed Touched Test 28-03-2013 28-03-2013 1 0.6667 100 Test2 28-03-2013 03-04-2013 ... (1 Reply)
Discussion started by: gangaraju6
1 Replies

3. Shell Programming and Scripting

Sum specified values (columns) per row

Hello out there, file.txt: comp51820_c1_seq1 42 N 0:0:0:0:0:0 1:0:0:0:0:0 0:0:0:0:0:0 3:0:0:0:0:0 0:0:0:0:0:0 comp51820_c1_seq1 43 N 0:0:0:0:0:0 0:1:0:0:0:0 0:0:0:0:0:0 0:3:0:0:0:0 0:0:0:0:0:0 comp51820_c1_seq1 44 N 0:0:4:0:3:1 0:0:1:9:0:0 10:0:0:0:0:0 0:3:3:2:2:6 2:2:2:5:60:3... (16 Replies)
Discussion started by: pathunkathunk
16 Replies

4. Shell Programming and Scripting

Sum value in a row and print the max

I have the input file in attached. I want the output file : Date , Time , Max_Bearer 11/01/2013 , 23:00 , 1447.894167 11/02/2013 , 00:00 , 1429.266667 11/03/2013 , 00:00 , 712.3175 11/04/2013 , 22:00 , 650.9533333 11/05/2013 , 23:00 , 665.9558333 11/06/2013 , 23:00 , 659.8616667... (2 Replies)
Discussion started by: justbow
2 Replies

5. Shell Programming and Scripting

getting the sum of numbers

I basically have a file where I had to do a bunch of greps to get a list of numbers example: a file called numbers.txt 10000 10000 superman 10000 batman 10000 10000 grep '100' * | 10000 10000 10000 10000 10000 (2 Replies)
Discussion started by: zerofire123
2 Replies

6. Shell Programming and Scripting

Finding the sum of two numbers

cat *.out |grep "<some text>" | awk '{print $6}' For ex,This will reutrn me 11111 22222 is it possible to add these two numbers in the above given command itself?I can write this to a file and find the sum. But I prefer to this calculation in the above given line itself. Any... (3 Replies)
Discussion started by: prasperl
3 Replies

7. Shell Programming and Scripting

Sum of values coming in a row

Hi, my requirement is to sum values in a row. eg: input is: sum,value1,value2,value3,.....,value N Required Output: sum,<summation of N values> Please help me... (5 Replies)
Discussion started by: MrGopal666
5 Replies

8. UNIX for Dummies Questions & Answers

Sum of data in row format

Hi All, I have some numbers in two different files file1 4.21927E+00 4.68257E+00 5.56871E+00 3.59490E+01 7.65806E+01 1.39827E+02 and file2 5.61142E+00 6.21648E+00 7.40152E+00 4.41917E+01 8.31586E+01 1.42938E+02 I would like to get file3 which contains in each column the sum of the... (6 Replies)
Discussion started by: f_o_555
6 Replies

9. Shell Programming and Scripting

sum numbers from stdout

hello im looking for short way to sum numbers from stdout the way i found to do it is to long for me i wander if there is shorter way to do it ok it 2 stage action this will make the list of number in to file sum.txt grep -c include *.c | awk '{l=split($0,a,":");print a;}' > sum.txt this... (1 Reply)
Discussion started by: umen
1 Replies

10. Shell Programming and Scripting

how to sum numbers in column

Hi, i want to sum all nubers in one column. Example: 12.23 11 23.01 3544.01 I'm trying to do this in awk, but it doesn't work properly. Seems like awk is summing only integers, for example: 12 11 23 3544 It cuts off numbers after dot. I used this command: akw /text/ file.txt |nawk... (1 Reply)
Discussion started by: iahveh
1 Replies
Login or Register to Ask a Question