Sum all rows with an Awk one-liner


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum all rows with an Awk one-liner
# 1  
Old 03-10-2010
Sum all rows with an Awk one-liner

I have a file with 1000+ columns of data. I need to sum each row (not column). How can I do this with an awk one-liner?

Thank you

Example file:

1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3

The output should be:
10
20
30

Last edited by jm4smtddd; 03-10-2010 at 05:34 PM.. Reason: More information was requested
# 2  
Old 03-10-2010
Please give a sample of your input file, otherwise we've no idea how to write any awk, one line or otherwise. And a clearer description of your requirement would help.

You have to sum each row how? Column 1 of row 1 with column 1 of row 2, or....?

Thanks.
# 3  
Old 03-10-2010
I'm feelin' lucky, so here's a shot in the dark:
Code:
awk '{for(i=1;i<=NF;i++) t+=$i; print t; t=0}'

Shrinking it a bit further at clarity's expense:
Code:
awk '{for(i=t=0;i<NF;) t+=$++i; $0=t}1'

If these are of no use, oh well .. it's the thought that counts Smilie

Alister

Last edited by alister; 03-10-2010 at 05:31 PM..
# 4  
Old 03-10-2010
Smilie I saw your post, and thought... I'll give him a second to add code tags... and, of course you did Smilie

Nice job with such little info to go on Smilie
# 5  
Old 03-10-2010
Thanks for the swift response.
# 6  
Old 03-10-2010
-EDIT- did not see the edited part ^^
Assuming 'data' is yout datafile and numbers are separated with a comma:
Code:
awk '{ for(i=1; i<=NF;i++) j+=$i; print j; j=0 }' data

seems to do the job.
Code:
unix.com$ cat data
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

unix.com$ awk '{ for(i=1; i<=NF;i++) j+=$i; print j; j=0 }' data
15
40
65
0


Last edited by tukuyomi; 03-10-2010 at 05:44 PM..
# 7  
Old 03-10-2010
Hi, scottn:

Heheheh. When I saw that I'd forgotten the code tags, and saw your post just above, I remembered how many times I've seen your name in edits and figured I'd better get on that ASAP Smilie

Cheers,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum of range of rows and columns in matrix

Hi all, I have a large matrix of 720 x 25. I want to get sum of range of rows and columns. Like, I need sum of all columns and row number 2 to 21, then leaving 22nd row, again sum of all columns and row number 23 to 42 again leaving 43rd row and then sum of 44th to 63. Means I want to add all... (4 Replies)
Discussion started by: CAch
4 Replies

2. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

3. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

4. Shell Programming and Scripting

Awk one-liner?

Hello, I have two files... File #1 1 3 2 5 File #2 3 5 3 1 3 7 9 1 5 2 5 8 3 3 1 I need to extract all lines from File #2 where the first two columns match each line of File #1. So in the example, the output would be: 1 3 7 2 5 8 Is there a quick one-liner that would... (4 Replies)
Discussion started by: palex
4 Replies

5. UNIX for Dummies Questions & Answers

Sum the rows number based on first field string value

Hi, I have a file like this one h1 4.70650E-04 4.70650E-04 4.70650E-04 h2 1.92912E-04 1.92912E-04 1.92912E-04 h3A 3.10160E-11 2.94562E-11 2.78458E-11 h4 0.00000E+00 0.00000E+00 0.00000E+00 h1 1.18164E-12 2.74150E-12 4.35187E-12 h1 7.60813E-01 7.60813E-01 7.60813E-01... (5 Replies)
Discussion started by: f_o_555
5 Replies

6. UNIX for Dummies Questions & Answers

need an awk one liner

example input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 (3 Replies)
Discussion started by: kenneth.mcbride
3 Replies

7. UNIX for Dummies Questions & Answers

how to count number of rows and sum of column using awk

Hi All, I have the following input which i want to process using AWK. Rows,NC,amount 1,1202,0.192387 2,1201,0.111111 3,1201,0.123456 i want the following output count of rows = 3 ,sum of amount = 0.426954 Many thanks (2 Replies)
Discussion started by: pistachio
2 Replies

8. UNIX for Dummies Questions & Answers

awk one liner

I need a one liner to" find /pattern/ print from x lines before "pattern" to y lines after "pattern" (3 Replies)
Discussion started by: kenneth.mcbride
3 Replies

9. Shell Programming and Scripting

awk one liner

input a 100 200 300 b 400 10 output a 100 a 200 a 300 b 400 b 10 Thanx (6 Replies)
Discussion started by: repinementer
6 Replies

10. UNIX for Dummies Questions & Answers

sum of all matching rows using awk

I have file "1","x1897"," 89.10" "1","x2232"," -12.12" "1","x1897"," 389.10" "1","x2232"," 212.12" "1","x1897"," 19.10" "1","x2232"," 2.12" i want to add all 3 rd column rows (they have spaces also)for x1 and sum of 3rd column rows for x2 separately. I am very... (8 Replies)
Discussion started by: i.scientist
8 Replies
Login or Register to Ask a Question