On Thu, Jan 15, 2009 at 02:41:24PM +0000, Andreas Kahari spoke thusly:
> 2009/1/15 igor denisov <denisovigor1...@rambler.ru>:
> > Hi there
> > Can not understand.
> >
> > input:
> > 34523 9348 98493 82983
> > 9485 83928 9283 9283
> > 394 39934 293 8347
> > 3456 9238 9283 9283
> >
> > awk 'NR==1 { for (i = 1; i <= NF; i++) {n=$i; next}}; {n-=$i} END {print n}'
> > input
> >
> > output:
> > 21188 it is first column, why?
> 
> You should really take these questions to an awk forum, not to this
> mailing list.
> 
> Your program reads the first record, and assigns its first column to
> 'n' in a loop that is immediately exited (with 'next') and goes on to
> read the remaining records.
> 
> For the remaining records, 'i' being still 1 from the prematurely
> exited loop, the first column is subtracted from 'n'.
> 
> Regards,
> Andreas
> 
> 
> -- 
> Andreas Kahari
> Somewhere in the general Cambridge area, UK
> 

Deleted original message, so replying to Andreas' message.

To sum by columns, do:
awk '{for(i=1;i<=NF;i++)t[i]+=$i}END{for(i=1;i in t;i++)print t[i]}' \
input

If you want to sum by lines, do:
awk '{for(i=1;i<=NF;i++)t[i]+=$i}END{for(i=1;i in t;i++)print t[i]}' \
input

If you want to total all the numbers at once you can do:
awk '{for(i=1;i<=NF;i++)t[i]+=$i}END{for(i=1;i in t;i++)print t[i]}' \
input

If I misunderstood what you want, like do you just want to total
the first column, then:

awk '{print $1}' input | \
awk '{for(i=1;i<=NF;i++)t[i]+=$i}END{for(i=1;i in t;i++)print t[i]}'

For another column, just change the '{print $1}' to whatever column
you need. The gurus might and probably do know a better way, but
that works. Hope this helps.


Denny White

-- 

 /"\    ASCII Ribbon Campaign
 \ /    Respect for low technology.
  X     Keep e-mail messages readable by any computer system.
 / \    Keep it ASCII.
===============================================================
GnuPG key  : 0x1644E79A  |  http://wwwkeys.nl.pgp.net
Fingerprint: D0A9 AD44 1F10 E09E 0E67  EC25 CB44 F2E5 1644 E79A
===============================================================

Reply via email to