2

How to pass a variable to regular expression like this?

match($0, /([^ ]+) (GET|POST|PUT|DELETE) ([^?]+)[^ ]+ HTTP\/[^ ]+ \"(VAR)\" ([^ ]+) ([^ ]+) ([^ ]+)/, matches)
unwind
  • 391,730
  • 64
  • 469
  • 606
exabiche
  • 2,895
  • 2
  • 17
  • 10

2 Answers2

1

I am not sure what you are trying to match, but if it is something along the lines of:

       GETxxxHTTPxxx"VAR"xxx

I think that you may go this way:

{
 var1="(VAR)"
 var="(GET|POST|PUT|DELETE)[x]+(HTTP)[x]+\"" var1 "\"[x]+"
 match($0,var,dd);
 for (x in dd){
    print x,"-->",dd[x]
    print"-"
 }
}

Which with the input above, produces the following output:

0start --> 1
-
0length --> 21
-
3start --> 15
-
1start --> 1
-
2start --> 7
-
0 --> GETxxxHTTPxxx"VAR"xxx
    -
1 --> GET
-
2 --> HTTP
-
3length --> 3
-
3 --> VAR
-
2length --> 4
-
1length --> 3
-

Running at ideone here

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
0

If you want to pass a shell variable into awk, use the -v option, like this:

awk -v VAR=${ShellVAR} '{
    //you awk program...
}' 
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
Fei
  • 1,450
  • 1
  • 17
  • 30