2 files: a loop within a loop?? 
Author Message
 2 files: a loop within a loop??

Hi,
I have two 4 column .csv (comma seperated values) format files for
which I am trying to do the following:

while file1 has more records to look through
   scan file2 to see if $1 $2 & $3 match file1's first 3 fields
      if so, do some math on $4 and write the answer to answer_file
        like so: $1,$2,$3,($answer=$4 + $4_from_file1)

   if no match is found anywhere in file2:
      write the current record (from file1) to answer_file like
         this: $1,$2,$3,$4

Right now, I might get a match, but then the ALL the other lines in
file2 are showing as unmatched and ending up in the answer_file for
each of the iterations through file1... which is wrong!

Do I need to use a shell script for the "outside" loop, in order to
test whether there was a match during the hunt in the awk (inside)
loop, or can I use awk alone?

I feel like the answer is right in front of me... but I'm frazzled.

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Mon, 30 Dec 2002 03:00:00 GMT  
 2 files: a loop within a loop??
Hi Bfreebs
how large are the files (at least in relation to your
computing reseources)?  If they aren't too big, I'd do this...
BEGIN{FS=","}
ARGIND==1 {
  file1[$1 "," $2 "," $3]=$4+0
  next
Quote:
}

{
  answer=$4+0
  key=$1 "," $2 "," $3
  if(key in file1) {answer+=file1[key]}
  print key "," answer

Quote:
}

Of course you could just do this all on a command line, but
thought we could practice our indentation today :)
And yes, I know this isn't the most efficient way to do this
but is is a nice example - and uses awk

Jennifer
--

Quote:

> Hi,
> I have two 4 column .csv (comma seperated values) format files for
> which I am trying to do the following:

> while file1 has more records to look through
>    scan file2 to see if $1 $2 & $3 match file1's first 3 fields
>       if so, do some math on $4 and write the answer to answer_file
>         like so: $1,$2,$3,($answer=$4 + $4_from_file1)

>    if no match is found anywhere in file2:
>       write the current record (from file1) to answer_file like
>          this: $1,$2,$3,$4

> Right now, I might get a match, but then the ALL the other lines in
> file2 are showing as unmatched and ending up in the answer_file for
> each of the iterations through file1... which is wrong!

> Do I need to use a shell script for the "outside" loop, in order to
> test whether there was a match during the hunt in the awk (inside)
> loop, or can I use awk alone?

> I feel like the answer is right in front of me... but I'm frazzled.

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Tue, 31 Dec 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. using while loops within while loops?

2. Problem with file input within nested loops

3. break one loop and skip one iteration of outer loop

4. Problem with loop inside other loop

5. Impelement for loop and do while loop

6. Fast loop inside a slow loop?

7. Troubles using while loops and case loops

8. For Loop looping only once

9. nested while loops, inside loop not stopping correctly

10. For loop, possible to increment counter/exit loop?

11. changing the value of loop-control variable in loop

12. Loop variable value after loop finish

 

 
Powered by phpBB® Forum Software