Replacing text containing parenthesis

M

Mark Hobley

I am using perl as a find and replace tool. I want to replace the following
text with a null:

$(srcdir)/config/override.m4

I tried the following:

find ./ -name Makefile.in -exec perl -pi -e \
"s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;" {} \;

Unfortunately, this gives an error:

Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE top_srcdir\)/\.\./ \
config/override\.m4/ at -e line 1, <> line 1.

I guess that the brackets are being evaluated first, and being treated as part
of the expression.

How do I represent literal brackets within the expression?

Mark.
 
A

A. Sinan Unur

(e-mail address removed) (Mark Hobley) wrote in
I am using perl as a find and replace tool. I want to replace the
following text with a null:

$(srcdir)/config/override.m4

I tried the following:

find ./ -name Makefile.in -exec perl -pi -e \
"s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;" {} \;

Unfortunately, this gives an error:

Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE
top_srcdir\)/\.\./ \ config/override\.m4/ at -e line 1, <> line 1.

I guess that the brackets are being evaluated first, and being treated
as part of the expression.

No. The shell gets to see the string first, replacing \$\( with $\(
where $\ is the output record separator.

So, replacing \$\( with \\$\( should work (untested).

Note also that the string you gave:

$(srcdir)/config/override.m4

will not match the pattern you use above.

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
J

John W. Krahn

Mark said:
I am using perl as a find and replace tool. I want to replace the following
text with a null:

$(srcdir)/config/override.m4

I tried the following:

find ./ -name Makefile.in -exec perl -pi -e \
"s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;" {} \;

Unfortunately, this gives an error:

Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE top_srcdir\)/\.\./ \
config/override\.m4/ at -e line 1, <> line 1.

Your string:

"s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;"

is in double quotes so it is interpolated by the shell first.

Run:

echo "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;"

from the shell to see how the shell interpolates it.


You probably want to use single quotes instead of double quotes.




John
 
M

Mark Hobley

John W. Krahn said:
Run:

echo "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;"
from the shell to see how the shell interpolates it.

Hmmm, it appeared to be ok ...

echo "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;"
s/$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;

However, you are right about those double quotes. I will try that, and see
what happens.

Mark.
 
M

Mark Hobley

Ben Morrow said:
Using single quotes for the shell will remove one level of
interpolation, and make things simpler. Using alternative delimiters for
the s/// will help get rid of some of those backwacks, and using \Q will
get rid of most of the rest:

... -e 's!\Q\$(top_srcdir)/../config/override.m4!!g;' \;

That is interesting. What is the capital Q?

Mark.
 
J

John W. Krahn

Mark said:
Hmmm, it appeared to be ok ...

echo "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;"
s/$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;

You are missing the \ in front of the $ so perl interpolates that as the
variable $\ and the left parenthesis is therefore not escaped generating
the error message "Unmatched ( in regex;".


John
 
S

sln

I am using perl as a find and replace tool. I want to replace the following
text with a null:

$(srcdir)/config/override.m4

I tried the following:

find ./ -name Makefile.in -exec perl -pi -e \
"s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;" {} \;

Unfortunately, this gives an error:

Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE top_srcdir\)/\.\./ \
config/override\.m4/ at -e line 1, <> line 1.

I guess that the brackets are being evaluated first, and being treated as part
of the expression.

How do I represent literal brackets within the expression?

Mark.

You know what? I FIND it hard to believe you type this from a command shell.
And I find it hard to believe you maintain a battery of batch files to shell
to perl scripts quoted from a batch file to be shelled to Perl from a shelled
shell.

-sln
(shell shocked!)
 
E

Eric Pozharski

(plz, don't get that as ranting) I've seen you on USENET for some time
already, you're not supposed to ask such, excuse me, stupid questions.

*SKIP*
find ./ -name Makefile.in -exec perl -pi -e \
"s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;" {} \;

By an accident your I<-e> oneliner becomes

{57028:35} [0:0]$ echo "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;"
s/$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;
Unfortunately, this gives an error:

Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE top_srcdir\)/\.\./ \
config/override\.m4/ at -e line 1, <> line 1.

Not exactly. I<$\> is a special variable for Perl, look it in
C<perldoc perlvar>. By default it's B<undef>ined, so evaluates to empty
string, and since you don't C<use warnings;> (or I<-w> switch for
oneliners) you stay unaware of this.

Advice: get used to single quotes when writing one-liners.

As of leaning-teeth-sticks:

's{\$\(top_srcdir\)/../config/override\.m4}{}g'

*CUT*
 
A

A. Sinan Unur

(e-mail address removed) (Mark Hobley) wrote in
Hmmm, it appeared to be ok ...

echo "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;"
s/$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;

How is that OK?

Now, instead of \$\( you have $\(

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top