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

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.

I'm feelin' lucky, so here's a shot in the dark:

awk '{for(i=1;i<=NF;i++) t+=$i; print t; t=0}'

Shrinking it a bit further at clarity's expense:

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 :wink:

Alister

:smiley: I saw your post, and thought... I'll give him a second to add code tags... and, of course you did :slight_smile:

Nice job with such little info to go on :slight_smile:

Thanks for the swift response.

-EDIT- did not see the edited part ^^
Assuming 'data' is yout datafile and numbers are separated with a comma:

awk '{ for(i=1; i<=NF;i++) j+=$i; print j; j=0 }' data

seems to do the job.

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

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 :wink:

Cheers,
Alister

Via bash

# while read line;do echo $((${line// /+}));done < infile
10
20
30

Another one, since it probably doesn't have to be awk after all

tr \  '+' < infile | bc

Thanks for the multiple options.

If blank lines should be handled and summed to zero:

tr \  '+' < infile | sed 's/^$/0/' | tr \  '+' | bc

Alister

One in perl,

perl -lane 'foreach (@F) { $s+=$_; } print $s;' file

You forgot to zero s for each line read.

Alister

@Alister,

Yes..It was a mistake...Thanks for pointing it out.

perl -lane '$s=0; foreach (@F) { $s+=$_; } print $s;' file