0

I am using the below single line command for replacing words

perl -i -p -e 's/old/new/g;' *.config

which works fine for normal string. However if i want to replace a string with special characters like below it doesn't work.

perl -i -p -e 's/{{'TEXT' | translate}}/{{'TEXT.T.D' | translate}}/g;' *.config

oldText will be like = {{'TEXT' | translate}}

New text will be like = {{'TEXT.T.D' | translate}}

3
  • 1
    s/\Q{{'TEXT' | translate}}/../check perldoc -f quotemeta Commented May 8, 2017 at 15:21
  • hai Сухой27 , thanks for the valuable comment . I tried perl -i -p -e 's/\Q{{'TEXT' | translate}}/{{'TEXT.T.D' | translate}}/g;' *.config ...However it doesn't replace the old value Commented May 8, 2017 at 17:41
  • Your actual code and/or data differs from what you posted above. => eval.in/790023 Commented May 8, 2017 at 19:14

1 Answer 1

1

The | symbol is a metacharacter in regular expressions (it means "or"). In order to use it to mean itself, you need to escape it with a backslash.

s/{{'TEXT' \| translate}}/{{'TEXT.T.D' | translate}}/g

Another alternative (which will escape all potentially problematic characters in the regex) is to use the \Q ("quotemeta") escape sequence.

s/\Q{{'TEXT' | translate}}/{{'TEXT.T.D' | translate}}/g
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.