|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
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
|
|||
|
|||
|
Thanks for the swift response.
|
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
-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 }' dataseems 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
0Last edited by tukuyomi; 03-10-2010 at 04:44 PM.. |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
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 | ||
|
|
![]() |
| Tags |
| awk, sum row |
| Thread Tools | Search this Thread |
| 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 |
|
|