2

How to pass a variable to regular expression like this?

match($0, /([^ ]+) (GET|POST|PUT|DELETE) ([^?]+)[^ ]+ HTTP\/[^ ]+ \"(VAR)\" ([^ ]+) ([^ ]+) ([^ ]+)/, matches)
0

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

awk -v VAR=${ShellVAR} '{
    //you awk program...
}' 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.