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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variables with metacharacters in them - help pls

Status
Not open for further replies.

myersg

Technical User
Nov 13, 2002
21
GB
I have the following script....
if(!found && match($0, PATstart)) {
found=1;
printf("%s", substr($0, 1, RSTART-1));
next;
}

if( found && match($0, PATend)) {
printf("%s%s\n", PATsubst, PATend);
if (RSTART+RLENGTH+1 < length($0))
printf(&quot;%s\n&quot;, substr($0, RSTART+RLENGTH+1));
found=0;
next;
}

if (!found) print
}

the idea is to pass it a file of SQL statements and strip out junk at the end and replace it with some standard text.

eg.

awk -f ${awk_cmd} &quot;PATstart=\&quot;PARTITION BY\&quot;&quot; \
&quot;PATend=\&quot;;\&quot;&quot; &quot;PATsubst=\&quot;\&quot;&quot; ${create_script}_table >>${create_script}_table2

This works fine - any multiline command that has PARTITION BY in it gets replaced with just a semicolon.

However if my PATstart contains, say a open bracket, I get mismatched () - it seems to be interpretng the bracket inside the quotes
....

awk -f ${awk_cmd} &quot;PATstart=\&quot;LOCAL(PARTITION\&quot;&quot; \
&quot;PATend=\&quot;;\&quot;&quot; &quot;PATsubst=\&quot;\&quot;&quot; ${create_script}_table >>${create_script}_table2
I get...
@fawk: There is a regular expression error.
() imbalance.
The input line number is 1. The file is /tmp/./customer_import_25963.sql_index.
The source line number is 1.

I've also tried hardcoding an example in a dummy awk file...
$ cat glm2_awk.cmd
BEGIN { PATstart=&quot;PARTITION(BY&quot;;
PATend=&quot;;&quot;;
PATsubst=&quot;&quot;
}
{print PATstart;
if(!found && match($0, PATstart)) {
found=1;
printf(&quot;%s&quot;, substr($0, 1, RSTART-1));
next;
}

if( found && match($0, PATend)) {
printf(&quot;%s%s\n&quot;, PATsubst, PATend);
if (RSTART+RLENGTH+1 < length($0))
printf(&quot;%s\n&quot;, substr($0, RSTART+RLENGTH+1));
found=0;
next;
}

if (!found) print
}

$ awk -f glm2_awk.cmd index2.sql
PARTITION(BY
awk: There is a regular expression error.
() imbalance.
The input line number is 1. The file is index2.sql.
The source line number is 6.


I'm new to awk so any help would be appreciated
 
Try a backslash in front of the open parenthesis

awk -f ${awk_cmd} &quot;PATstart=\&quot;LOCAL\(PARTITION\&quot;&quot; \ CaKiwi
 
Thanks,

However it seems to have nothing to do with passing the pattern on the command line, as the second example the pattern is hard coded in the BEGIN section and it still complains about the open parentheses.

I still tried what you've said though, and it still fails
 
Consider this simple test.

#--- file myersg.dat ---
aaa(bbb)ccc
aaa(bbb)ddd
aaa(bbb)eee

#--- file myersg.awk ---
{
ss = &quot;aaa(bbb&quot;
if (match($0,ss)) print
}

Run by entering

awk -f myersg.awk myersg.dat

You should get an error noting a parenthesis imbalance.

Now add a backslash to the match pattern


#--- file myersg.awk ---
{
ss = &quot;aaa\(bbb&quot;
if (match($0,ss)) print
}

It should run without error. CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top