Results 1 to 5 of 5
Thread: Working with columns

060204, 12:23 #1Registered User
 Join Date
 Feb 2004
 Posts
 52
Unanswered: Working with columns
Hi all.
Could someone shed some light on this problem ?
I have one file with N columns composed by integer numbers.
All columns have the same number of lines. It is something like this :
1 4 12 4 7 2 9 4 ...
1 3 5 36 8 8 4 2 ...
1 4 5 5 6 77 8 89 ...
4 0 2 5 1 2 4 5 ...
...
...
1 3 5 2 1 1 2 32 ...
Given one number (let's say N), for each column I need to sum
the corresponding numbers each N lines. For instance, using N = 2
and the list above the result must be :
2 7 17 40 15 10 13 6 < sum over the first two lines
5 4 7 10 7 79 12 94 < sum over third and fourth lines
...
...
I know how to handle one single column but the real
problem contains more than 400 columns. That's annoying.
I appreciate any help !!!
Thanks,
Serg

060204, 17:14 #2Registered User
 Join Date
 Apr 2004
 Location
 Boston, MA
 Posts
 325
what is the meaning of 'N'?
sum of every N lines? as in N=2 => every 2 lines; N=3 => every 3 lines etc?
or is it something else?

060204, 18:04 #3Registered User
 Join Date
 Apr 2004
 Location
 Boston, MA
 Posts
 325
Here's one way to implement what I THINK you're trying to get
# to sum up every TWO lines
nawk f serg.awk testFile.txt
# to sum up every THREE lines
nawk v num=3 f serg.awk testFile.txt
# to sum up every FIVE lines
nawk v num=5 f serg.awk testFile.txt
Code:BEGIN { if ( num == "") num=2 } { for(i=1; i <= NF; i++) arr[i] +=$i } !(FNR % num) { for(i=1; i <= NF; i++) { printf("%s%s", arr[i], (i==NF) ? "\n" : OFS); delete arr[i]; } }

060304, 09:53 #4Registered User
 Join Date
 Apr 2004
 Location
 Boston, MA
 Posts
 325
or better yet:
Code:BEGIN { if ( num == "") num=2 } { for(i=1; i <= NF; i++) arr[i] +=$i fld=NF; } !(FNR % num) { for(i=1; i <= NF; i++) { printf("%s%s", arr[i], (i==NF) ? "\n" : OFS); delete arr[i]; } } END { if (FNR % num) for(i=1; i <= fld; i++) printf("%s%s", arr[i], (i==fld) ? "\n" : OFS); }

060304, 15:38 #5Registered User
 Join Date
 Feb 2004
 Posts
 52
Thanks for the replies, Vlad.
"what is the meaning of 'N'?
sum of every N lines? as in N=2 => every 2 lines; N=3 => every 3 lines etc?"
Ups ! My mistake ! In my original post I used
"N" for columns AND number of lines. My mistake.
N is the number of lines to be counted. You guessed right.
I am going to test your scripts right away.
Thanks a lot,
Serg