Allan Bowe
Allan Bowe

Reputation: 12701

perl find and replace with single quotes

I need to find / replace the following string in all the .txt files in my working directory:

myfunc(l_define,'BASE TABLE')

I've tried the following, which runs without errors, but doesn't update the file(s).

perl -pi -w -e "s/myfunc(l_define,'BASE')/myfunc(l_define,'BASE',1,1,'')/g;" *.txt

Is it a quoting issue?

Upvotes: 1

Views: 229

Answers (1)

LukStorms
LukStorms

Reputation: 29677

In a regex, what's between ( and ) is a capture group.

So for the ( or ) characters you need to escape them, so that the literal characters are matched. So use \( and \) in the regex.

Hence, a regex like func(akes) would only match funcakes.

Escaping is often needed to match characters that have a special meaning in the regex syntax:
. ( ) [ ] + * ? { } ^ $ \ |
Also / if the code uses something like s/a\/b/a-b/g instead of something like s@a/b@a-b@g

And if you really don't like to use those backslashes?
You could also use a character class: [(] and [)]

Upvotes: 3

Related Questions