I have two Perl one liners:
perl -pe "s/\b$a\b/$b/g if m/param1 /" test
and
perl -pe "s/\b$a\b/$b/g unless /^#/" test
How can I combine theif m/somthing/ and the unless /something/, like:
[root@localhost tmp]# perl -pe "s/\b$a\b/$b/g if m/param1/ unless /^#/" test
syntax error at -e line 1, near "m/param1/ unless"
On an unrelated note, you may want to add the
\Qand\Eescape sequences around$ain your regex:They escape any characters that are special to regexes. If you intend for
$ato hold a regex you should probably move the word boundary assertions (\b) into it.No matter what you choose to do, you will need to be careful with values in
$aand$b. For instance:will cause a syntax error. One solution to this is to not use environment variables to replace code. Perl allows you access to the environment through the
%ENVhash:Notice the use of single ticks to avoid treating
$ENVas a shell variable.