Quoting does not work in replace?

H

Hans Deragon

Greetings.


I want to parse a file and replace a string with another, in my
example, replace @sysconfdir@ with /etc. The command:

perl -wp -e "s/\@sysconfdir\@/\Q/etc\E/;" <file>

....and off course, the error (else I would not be writing here ;) ):

Bareword found where operator expected at -e line 1, near
"s/\@sysconfdir\@/\Q/etc"
Unquoted string "tc" may clash with future reserved word at -e line 1.
Backslash found where operator expected at -e line 1, near "tc\"
syntax error at -e line 1, near "s/\@sysconfdir\@/\Q/etc"
Execution of -e aborted due to compilation errors.

Anybody would care to explain why \Q does not work here, and what I
can do to fix it? I would like a one liner solution. Manually
backquoting / ("\/") does not work either.

If / in /etc is removed, it works as expected. / is simply not
quoted.


Best regards,
Hans Deragon
 
M

Matt Garrish

Hans Deragon said:
Greetings.


I want to parse a file and replace a string with another, in my
example, replace @sysconfdir@ with /etc. The command:

perl -wp -e "s/\@sysconfdir\@/\Q/etc\E/;" <file>

...and off course, the error (else I would not be writing here ;) ):

Bareword found where operator expected at -e line 1, near
"s/\@sysconfdir\@/\Q/etc"
Unquoted string "tc" may clash with future reserved word at -e line 1.
Backslash found where operator expected at -e line 1, near "tc\"
syntax error at -e line 1, near "s/\@sysconfdir\@/\Q/etc"
Execution of -e aborted due to compilation errors.

Anybody would care to explain why \Q does not work here, and what I
can do to fix it? I would like a one liner solution. Manually
backquoting / ("\/") does not work either.

Works fine for me if I escape the slash. And \Q doesn't escape the slash
because the slash is also your delimiter. You might think it would make
sense to escape it, but the \E is optional in a regex, so if it didn't
recognize the delimiter as a delimiter, well then the \E would no longer be
optional would it...

Matt
 
A

Anno Siegel

Hans Deragon said:
Greetings.


I want to parse a file and replace a string with another, in my
example, replace @sysconfdir@ with /etc. The command:

perl -wp -e "s/\@sysconfdir\@/\Q/etc\E/;" <file>

...and off course, the error (else I would not be writing here ;) ):

Bareword found where operator expected at -e line 1, near
"s/\@sysconfdir\@/\Q/etc"
Unquoted string "tc" may clash with future reserved word at -e line 1.
Backslash found where operator expected at -e line 1, near "tc\"
syntax error at -e line 1, near "s/\@sysconfdir\@/\Q/etc"
Execution of -e aborted due to compilation errors.

Anybody would care to explain why \Q does not work here, and what I
can do to fix it? I would like a one liner solution. Manually
backquoting / ("\/") does not work either.

It should. In fact, it does for me.

/Q and friends work in double-quotish context, but in substitutions like
yours "/" delimits that context, but isn't part of it. Only direct
backwhacking helps with delimiters.

That's why Perl lets you chose the delimiter. Write it

s{\@sysconfdir\@}{/etc}

or, since you don't do interpolation

s'@sysconfdir@'/etc'

with no quoting at all.

Anno
 
T

Tad McClellan

Hans Deragon said:
perl -wp -e "s/\@sysconfdir\@/\Q/etc\E/;" <file>
Bareword found where operator expected at -e line 1, near
"s/\@sysconfdir\@/\Q/etc"

Anybody would care to explain why \Q does not work here,


Perl never got far enough in its parse to recognize the \Q.

It was looking for the extent of the 2 parts of the s///,
_then_ it would turn to interpolating whatever ended up
in the parts.

and what I
can do to fix it?


It should have never even happened. :)

You should consider choosing alternate delimiters for m// and s///
where one of the parts is to contain a literal slash.

I like s### for one-liners:

perl -wp -e "s#\@sysconfdir@#/etc#"

Manually
backquoting / ("\/") does not work either.

perl -wp -e "s/\@sysconfdir@/\/etc/"

It works for me. Are you sure you were backslashing the right slash?

(that confusion is not even possible with alternate delimiters...)
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,184
Latest member
ZNOChrista

Latest Threads

Top