AWK nesting and Regular Expression

I want to use one file as the source variable for my regular expressions  , while I awk another file. The idea is to have a new regular expression ($i) applied to a given file.

I want to use awk to solve this problem:

See code snipet for where I am stuck.

Thank you

PA

awk '
{ for ( i = 1; i <= NF; i++ )
     # I want to do :  /enterprises.9.9.166.1.5.1.1.2./  && $0~i$  on file ./MIBDump 
}' class_index

Open in new window

pierre-alexAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
what do you mean by
$0~i$  on file ./MIBDump
do you want $0 to come from  ./MIBDump?
do you want $i to come from /MIBDump?
and it looks like the expression just returns true or false.  do you actually want to do anything?
0
pierre-alexAuthor Commented:
Ozo,


 I want to run a reg expression which looks for the occurence of /enterprises.9.9.166.1.5.1.1.2./ in the file "MIBDump" AND the occurence of $i in the file "class_index".

so lets suppose in the file class_index I have two values: 33151 and 39891 then the script will run a search for :

/enterprises.9.9.166.1.5.1.1.2./ && /33151/
then

/enterprises.9.9.166.1.5.1.1.2./ && /39891 /


The search will be done on file "MIBDump".


( $0~$i  purpose is to  fetch an entire line of the file class_index and put it in variable $i  )  

Thanks

PA


0
JIEXACommented:
I'd concatenate the 2 files with separator. The code below is not tested, but you can understand what's done.
(cat expression.txt ; echo SEPARATOR ; cat data.txt ) | awk '
BEGIN{separatorSeen=0}
(separatorSeen==0){expression=$0}
/^SEPARATOR$/{separatorSeen=1}
(separatorSeen==1 && ...){  if ($0~expression) ... }'

Open in new window

0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

pierre-alexAuthor Commented:
JIEXA

Thanks for the reply, but the files need to stay separate.

Regards

PA
0
JIEXACommented:


cat data.txt | awk '
BEGIN{
  getline < "expression.txt"
  expression=$0
}
{  if ($0~expression) ... }'

Open in new window

0
ozoCommented:
If I am understanding what you are saying, it sounds like you are asking for
grep -F enterprises.9.9.166.1.5.1.1.2. MIBDump  | grep -f class_index
0
pierre-alexAuthor Commented:
Ozo, yes this is correct but it needs to be done in awk ...

Thank you

PA
0
pierre-alexAuthor Commented:
JIEXA:

Using "getline" seems a good idea, however, when used in the "BEGIN" section, only the FIRST expression in the file "expression.txt" is retrieved.

I have tried using "getline" in the main body of awk, but the behaviour is as follows:

For each line in the file "data.txt" the NEXT line in file "expression.txt" is retrieved and used as a regular expression, so what I end up having is the FIRST line of "data.txt" against the "second line of "expression.txt" then the SECOND line of "data.txt" agains the "third line of expression.txt " etc ....

This is not what I want, but its seems to be the natural behaviour of "getline" according to the documentation.

What I need is for each line of "data.txt" match the first expression in "expression.txt", then for each line of "data.txt"  match the second expression in "expression.txt",  then for each of "data.txt" match the third expression etc ...

Can this be accomplished with an awk program at the first place ???
 

thanks

PA
0
pierre-alexAuthor Commented:
JIEXA:

I spent some time rewriting the code to solve the problem using arrays.

(My attempt using "getline" was unfortunately fruitless, see previous post)

My new code is working  OK (see snippet), but I have to manually populate the array in the "BEGIN" section of 'AWK. That is awkward.


What I need is a way to populate populate my array arr[x] on initialization from a secondary file "class_index" ...

Working code, primary and secondary files all can be found below.

Any suggestions?

Regards

PA
 


awk '
BEGIN {
arr[1] = "102983"
arr[2] = "103019"
target_parent = "102975"
#print arr[1];print arr[2]
}
 
 
/enterprises.9.9.166.1.5.1.1.4/ {
 
 #print $0
 
 #}
 
     for ( i = 1; i<=2; i++ )
 
     { value = arr[i]
 
      # Trying to run a regular expression within a For loop _ Failed
      # /102975/ && $0~value
         # {
         #  print $0
         # }
 
      gsub(/.*\./,"",$1)
      child=$1
      #print child
 
      m=split($4,t,".")
      parent=t[m]
      #print "last value:"
      #print t[m]
 
      if ( child == arr[i] && target_parent == parent)
             {
                { print "parent:", parent }
                { print "child:", child }
              }
       }
 
                                 } ' MIBDump
 
Output:
 
parent: 102975
child: 102983

Open in new window

MIBDump
class-index
0
JIEXACommented:
You can use a loop for getline!
BEGIN {
  c=1
  while((getline < "expression.txt") > 0)
  {
    arr[c]=$0
    c++
  }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JIEXACommented:
Or if there are only 3 data lines:
BEGIN {
  file = "expression.txt"
  getline < file
  arr[1] = $0
  getline < file
  arr[2] = $0
  getline < file
  target_parent = $0
}
...

Open in new window

0
pierre-alexAuthor Commented:
JIEXA:

Brilliant! Working like a charm now.

Thanks for your help

PA
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.