Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Sum all rows with an Awk one-liner

Shell Programming and Scripting


Tags
awk, sum row

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-10-2010
jm4smtddd jm4smtddd is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 10 March 2016, 3:42 PM EST
Posts: 11
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 Unix and Linux 03-10-2010
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 24 March 2017, 8:26 PM EDT
Location: Zürich
Posts: 8,602
Thanks: 344
Thanked 1,098 Times in 950 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 Unix and Linux 03-10-2010
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 976 Times in 790 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 Linux

Alister

Last edited by alister; 03-10-2010 at 04:31 PM..
    #4  
Old Unix and Linux 03-10-2010
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 24 March 2017, 8:26 PM EDT
Location: Zürich
Posts: 8,602
Thanks: 344
Thanked 1,098 Times in 950 Posts
Linux I saw your post, and thought... I'll give him a second to add code tags... and, of course you did Linux

Nice job with such little info to go on Linux
Sponsored Links
    #5  
Old Unix and Linux 03-10-2010
jm4smtddd jm4smtddd is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 10 March 2016, 3:42 PM EST
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks for the swift response.
Sponsored Links
    #6  
Old Unix and Linux 03-10-2010
tukuyomi tukuyomi is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 27 September 2016, 1:25 AM EDT
Location: France
Posts: 320
Thanks: 19
Thanked 81 Times in 76 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 Unix and Linux 03-10-2010
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 976 Times in 790 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 Linux

Cheers,
Alister
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 03:37 PM.