Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk match substitution using regexp "^" and a variable name

Status
Not open for further replies.

francos3

Programmer
May 8, 2017
1
GB
Hi,

I want to do a gawk statement were the following is matched but it does not work:

variable="partners";
temp="(partners ?x1 ?x2";
if ( temp ~ /^\(/variable/ ?/ ){
print hello;
}

The following works, so the problem is getting the variable value of "partners" inside the regexp.

if ( temp ~ /^\(partners ?/ )
I tried using match function instead, but keep getting errors, e.g:
if (match(temp,"^\("variable" ?") ){
I get the error:
gawk: ../../../scripts/Hikin-3Dto2D-PDDL.awk:147: fatal: :, [., or [=: /^(partners ?/

To summarize, can I do a search in gawk that uses both regexp "^" for beginning of string and also a variable as part of the string to match???

Thanks!

 
Hi

You have to specify the regular expression as string to be able to concatenate it with a variable :
Code:
[b]if[/b] [teal]([/teal] temp [teal]~[/teal] [i][green]"/^[/green][/i][lime]\\[/lime][i][green]("[/green][/i] variable [i][green]" ?"[/green][/i] [teal]){[/teal]
Just watch out the escaping.


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top