views:

288

answers:

3

If I have a file containing some escaped parens, how can I replace all instances with an unescaped paren using Perl?

i.e. turn this:

.... foo\(bar ....

into this

.... foo(bar ....

I tried the following but receivied this error message:

perl -pe "s/\\\(/\(/g" ./file
Unmatched ( in regex; marked by <-- HERE in m/\\( <-- HERE / at -e line 1.
+11  A: 

You're forgetting that backslashes mean something to the shell, too. Try using single quotes instead of double quotes. (Or put your script in a file, where you won't need to worry about shell quoting.)

cjm
Gah, can't believe I didn't think of that. Friday can't come soon enough.
dmercer
+6  A: 

Gah. From command line, no less. Way too many levels of metacharacter interpretation.

Try replacing your double quotes with single quotes, see if that helps.

chaos
Thanks, you're about 8 seconds behind the earlier answer. Cheers
dmercer
A: 

cjm's answer is probably the best. If you must do it at the command line, try using quotemeta() or the metaquoting escape sequence (\Q...\E). This worked for me in a bash prompt:

perl -pe "s/\Q\(\E/(/g" ./file
gpojd