Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-10-2010
Registered User
 

Join Date: Mar 2010
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
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 04:34 PM.. Reason: More information was requested
Sponsored Links
    #2  
Old 03-10-2010
Scott's Avatar
Scott Scott is online now Forum Staff  
Administrator
 

Join Date: Jun 2009
Location: Switzerland - ZH
Posts: 5,321
Thanks: 128
Thanked 526 Times in 465 Posts
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.
Sponsored Links
    #3  
Old 03-10-2010
alister alister is offline Forum Advisor  
Registered User
 

Join Date: Dec 2009
Posts: 1,496
Thanks: 39
Thanked 308 Times in 270 Posts
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

Alister

Last edited by alister; 03-10-2010 at 04:31 PM..
    #4  
Old 03-10-2010
Scott's Avatar
Scott Scott is online now Forum Staff  
Administrator
 

Join Date: Jun 2009
Location: Switzerland - ZH
Posts: 5,321
Thanks: 128
Thanked 526 Times in 465 Posts
I saw your post, and thought... I'll give him a second to add code tags... and, of course you did

Nice job with such little info to go on
Sponsored Links
    #5  
Old 03-10-2010
Registered User
 

Join Date: Mar 2010
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks for the swift response.
Sponsored Links
    #6  
Old 03-10-2010
Registered User
 

Join Date: Aug 2009
Location: France
Posts: 236
Thanks: 10
Thanked 46 Times in 45 Posts
-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 04:44 PM..
Sponsored Links
    #7  
Old 03-10-2010
alister alister is offline Forum Advisor  
Registered User
 

Join Date: Dec 2009
Posts: 1,496
Thanks: 39
Thanked 308 Times in 270 Posts
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

Cheers,
Alister
Sponsored Links
Closed Thread

Tags
awk, sum row

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk one liner kenneth.mcbride UNIX for Dummies Questions & Answers 3 01-02-2010 12:39 PM
Deleting specific rows in large files having rows greater than 100000 manish2009 Shell Programming and Scripting 9 12-11-2009 12:17 PM
Converting rows into multiple-rows PHL UNIX for Dummies Questions & Answers 6 11-14-2009 05:26 AM
awk one liner repinementer Shell Programming and Scripting 6 10-01-2009 05:36 AM
3 files in one awk one liner kishal Shell Programming and Scripting 4 03-01-2009 04:16 PM



All times are GMT -4. The time now is 12:59 AM.