Sum of numbers in row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum of numbers in row
# 8  
Old 12-19-2016
Quote:
Originally Posted by Aia
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?
Different characters each time but always 4 rows before blank line begins..
# 9  
Old 12-19-2016
this simple script will add up numbers skipping every 5th line (as you have said, four rows of numbers always follow by a row we don't need to add)

Code:
#!/bin/bash
cnt=0;
sum=0;
while read n; do
        ((cnt = cnt + 1))
        if((cnt % 5)); then
                ((sum = sum + $n))
        fi
done < "$1"
echo $sum

As you see, there is no checking for bad input etc... just skip 5th line
# 10  
Old 12-20-2016
Quote:
Originally Posted by smitty11
Different characters each time but always 4 rows before blank line begins..
Please, try the following:

Code:
perl -nle 'END{print $s} /^(\d+)/ and $s+=$1 or print $s and $s=0' smitty.file

# 11  
Old 12-20-2016
The same a ronaldxs' suggestion, but add RS=:
Code:
awk '{sum=0; for(i=1;i<=NF;i++) sum+=$i; print sum}' RS= file


or
Code:
awk '{$1=$1}1' RS= OFS=+ file | bc


--
Alternative for the original question:
Code:
sed 's/  */+/g' file | bc


Last edited by Scrutinizer; 12-20-2016 at 08:42 AM..
# 12  
Old 12-20-2016
Hi Ronaldxs , Could you please explain below code.
Code:
perl -Mstrict -MList::Util=sum -wanE 'say sum(@F)'

# 13  
Old 12-20-2016
Works.
Thx to all.
# 14  
Old 12-20-2016
Quote:
Originally Posted by looney
Hi Ronaldxs , Could you please explain below code.
Code:
perl -Mstrict -MList::Util=sum -wanE 'say sum(@F)'

Code:
perl                       # perl binary
-M strict                 # use strict
-M List::Util=sum    # use List::Util qw(sum).To know what the subroutine sum is.
-w                          # use warnings
-a                           # split record into array @F
-n                          # loop each line
-E                          # Load all options for the command. Necessary to make use of say instead of print
' say sum(@F)'     # take the content of array F and sum them up and display result.

This User Gave Thanks to Aia For This Post:
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