Lookup function in awk - answer 
Author Message
 Lookup function in awk - answer

Hi all.  After many frustrating hours at solving my awk lookup
problem, I thought I'd share my solution with everyone.  This is a
very practical lookup script that I'm sure you will appreciate:

awk ' BEGIN {
               idx=0
               while ( getline < "external_file" )
               {
                  lookup[idx] = $1
                  idx++
               }
            }

      function CheckInUnitLookup(Unit)
      {
         for (idx in lookup)
         {
            if (Unit == lookup[idx])
            {
               return(1);
            }
         }

         return(0);
      }

      {
         if ( CheckInUnitLookup($NF) )
         {
            print "in array"
         }
         else
         {
            print "not in array"
         }
      }' main_input_file

I don't know if there's an actual awk function that validates data
from an array ( as opposed to determining if it simply exist in the
array ).  I must admit that I didn't come up with this answer entirely
on my own ;-)

DD



Sun, 05 Dec 2004 21:14:55 GMT  
 Lookup function in awk - answer

Quote:

> Hi all.  After many frustrating hours at solving my awk lookup
> problem, I thought I'd share my solution with everyone.  This is a
> very practical lookup script that I'm sure you will appreciate:

> awk ' BEGIN {
>                idx=0
>                while ( getline < "external_file" )
>                {
>                   lookup[idx] = $1
>                   idx++
>                }
>             }

>       function CheckInUnitLookup(Unit)
>       {
>          for (idx in lookup)
>          {
>             if (Unit == lookup[idx])
>             {
>                return(1);
>             }
>          }

>          return(0);
>       }

>       {
>          if ( CheckInUnitLookup($NF) )
>          {
>             print "in array"
>          }
>          else
>          {
>             print "not in array"
>          }
>       }' main_input_file

> I don't know if there's an actual awk function that validates data
> from an array ( as opposed to determining if it simply exist in the
> array ).  I must admit that I didn't come up with this answer entirely
> on my own ;-)

> DD

I would use awk's asociative array capability:

 awk ' BEGIN {
                idx=0
                while ( getline < "external_file" )
                {
                   lookup[$1] = idx
                   idx++
                }
             }

       function CheckInUnitLookup(Unit)
       {
        return (Unit in lookup) ? 1 : 0
       }

.....

Martin Cohen



Mon, 06 Dec 2004 02:16:49 GMT  
 Lookup function in awk - answer


Quote:

>> Hi all.  After many frustrating hours at solving my awk lookup
>> problem, I thought I'd share my solution with everyone.  This is a
>> very practical lookup script that I'm sure you will appreciate:

>> awk ' BEGIN {
>>                idx=0
>>                while ( getline < "external_file" )
>>                {
>>                   lookup[idx] = $1
>>                   idx++
>>                }
>>             }

>>       function CheckInUnitLookup(Unit)
>>       {
>>          for (idx in lookup)
>>          {
>>             if (Unit == lookup[idx])
>>             {
>>                return(1);
>>             }
>>          }

>>          return(0);
>>       }

>>       {
>>          if ( CheckInUnitLookup($NF) )
>>          {
>>             print "in array"
>>          }
>>          else
>>          {
>>             print "not in array"
>>          }
>>       }' main_input_file

>> I don't know if there's an actual awk function that validates data
>> from an array ( as opposed to determining if it simply exist in the
>> array ).  I must admit that I didn't come up with this answer entirely
>> on my own ;-)

>> DD

>I would use awk's asociative array capability:

> awk ' BEGIN {
>                idx=0
>                while ( getline < "external_file" )
>                {
>                   lookup[$1] = idx
>                   idx++
>                }
>             }

>       function CheckInUnitLookup(Unit)
>       {
>    return (Unit in lookup) ? 1 : 0
>       }

Or:

# Abort() is not included here, for brevity
function CheckInUnitLookup(Unit) {
        return Unit in lookup
        }
BEGIN   {
        fn = "external_file"
        if (getline < fn < 0)
            Abort("File: "fn" not found!")
        do lookup[$1]
        while (getline < fn)
        close(fn)
        }



Mon, 06 Dec 2004 02:34:27 GMT  
 Lookup function in awk - answer

Quote:




> >> Hi all.  After many frustrating hours at solving my awk lookup
> >> problem, I thought I'd share my solution with everyone.  This is a
> >> very practical lookup script that I'm sure you will appreciate:

> >> awk ' BEGIN {
> >>                idx=0
> >>                while ( getline < "external_file" )
> >>                {
> >>                   lookup[idx] = $1
> >>                   idx++
> >>                }
> >>             }

> >>       function CheckInUnitLookup(Unit)
> >>       {
> >>          for (idx in lookup)
> >>          {
> >>             if (Unit == lookup[idx])
> >>             {
> >>                return(1);
> >>             }
> >>          }

> >>          return(0);
> >>       }

> >>       {
> >>          if ( CheckInUnitLookup($NF) )
> >>          {
> >>             print "in array"
> >>          }
> >>          else
> >>          {
> >>             print "not in array"
> >>          }
> >>       }' main_input_file

> >> I don't know if there's an actual awk function that validates data
> >> from an array ( as opposed to determining if it simply exist in the
> >> array ).  I must admit that I didn't come up with this answer entirely
> >> on my own ;-)

> >> DD

> >I would use awk's asociative array capability:

> > awk ' BEGIN {
> >                idx=0
> >                while ( getline < "external_file" )
> >                {
> >                   lookup[$1] = idx
> >                   idx++
> >                }
> >             }

> >       function CheckInUnitLookup(Unit)
> >       {
> >       return (Unit in lookup) ? 1 : 0
> >       }

> Or:

> # Abort() is not included here, for brevity
> function CheckInUnitLookup(Unit) {
>         return Unit in lookup
>         }
> BEGIN   {
>         fn = "external_file"
>         if (getline < fn < 0)
>             Abort("File: "fn" not found!")
>         do lookup[$1]
>         while (getline < fn)
>         close(fn)
>         }

thats not awk! (like) euuuu! :)
jen


Mon, 06 Dec 2004 12:25:42 GMT  
 Lookup function in awk - answer
Hi Domenico...
that would run _very_ slowly...
as Martin suggested, you should use the associate array
stuff for this - it's very fast.
you can also avoid using the getline construct (with
the associated hard coded reference file name) by
feeding the reference file and the file to be checked
through the command line
eg awk -f program_file ref_file data_file
then you just sense which file you are in and either
build the reference array, or check the values against the
reference array.  in GAWK you can use the ARGIND variable
to see which file you are processing.  in AWK you can
check either the FILENAME itself, or if it has changed.
I often use the number of fields (NF) to tell (say if the
reference file has only one variable on a line, and the
data file has several)
so for gawk, your program (run as gawk -f program ref_file data_file)
ARGIND==1{ ref[$1]=1;next}
$NF in ref {print "in array";next}
{print "not in array"}

this should run very fast!
jen

Quote:

> Hi all.  After many frustrating hours at solving my awk lookup
> problem, I thought I'd share my solution with everyone.  This is a
> very practical lookup script that I'm sure you will appreciate:

> awk ' BEGIN {
>                idx=0
>                while ( getline < "external_file" )
>                {
>                   lookup[idx] = $1
>                   idx++
>                }
>             }

>       function CheckInUnitLookup(Unit)
>       {
>          for (idx in lookup)
>          {
>             if (Unit == lookup[idx])
>             {
>                return(1);
>             }
>          }

>          return(0);
>       }

>       {
>          if ( CheckInUnitLookup($NF) )
>          {
>             print "in array"
>          }
>          else
>          {
>             print "not in array"
>          }
>       }' main_input_file

> I don't know if there's an actual awk function that validates data
> from an array ( as opposed to determining if it simply exist in the
> array ).  I must admit that I didn't come up with this answer entirely
> on my own ;-)

> DD



Mon, 06 Dec 2004 12:36:01 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. awk lookup problem using getline

2. Fast lookup in awk

3. lookup files in awk - question?

4. Sed/awk question Answered!

5. Why won't lookup function? CW4

6. C API for member function lookup?

7. Function lookup using a directory.

8. CAPS Function anyone? - Answer

9. Function hosts _in_ ARexx, the answer

10. Answer - math functions in Verilog

11. Questions-Questions-Questions-Answers-Answers

 

 
Powered by phpBB® Forum Software