If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Working with columns

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-02-04, 11:23
Serg Serg is offline
Registered User
 
Join Date: Feb 2004
Posts: 52
Question 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
Reply With Quote
  #2 (permalink)  
Old 06-02-04, 16:14
vgersh99 vgersh99 is offline
Registered 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?
Reply With Quote
  #3 (permalink)  
Old 06-02-04, 17:04
vgersh99 vgersh99 is offline
Registered 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];
   }
}
Reply With Quote
  #4 (permalink)  
Old 06-03-04, 08:53
vgersh99 vgersh99 is offline
Registered 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);
}
Reply With Quote
  #5 (permalink)  
Old 06-03-04, 14:38
Serg Serg is offline
Registered 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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On