Is there some way to replace a string such as @or * or ? or & without needing to put a “\” before it?
Example:
perl -pe 'next if /^#/; s/\@d\&/new_value/ if /param5/' test
In this example I need to replace a @d& with new_value but the old value might contain any character, how do I escape only the characters that need to be escaped?
You have several problems:
\bincorrectlyFrom
perldoc perlreNeither of the characters
@or&are\wcharacters. So your match is guaranteed to fail. You may want to use something likes/(^|\s)\@d\&(\s|$)/${1}new text$2/(^|\s)says to match either the start of the string (^)or a whitespace character (\s).(\s|$)says to match either the end of the string ($) or a whitespace character (\s).To solve the second problem, you should use
%ENV.To solve the third problem, you should use the
\Qand\Eescape sequences to escape the value in$ENV{a}.Putting it all together we get:
Which prints