My target is to replace any word/string/Any characters in file with new word/string according to the following rules:If numeric characters on the left side of word/string then we not replace the wordIf numeric characters on the right side of word/string then we not replace the wordremark - numeric means any number or [a-z] or [A-Z] The following Example described file before , and file after:I want to replace the word $$OLD_WORD$$ with &&NEW_WORD&&example 1 -  file Before replacement   more file   ABC$$OLD_WORD$$ABC  $$OLD_WORD$$GG  $$OLD_WORD$$  1$$OLD_WORD$$example 2 - file after replacement  more file  ABC$$OLD_WORD$$ABC  $$OLD_WORD$$  &&NEW_WORD&&  1$$OLD_WORD$$So in order to replace the word regarding the rules (1,2) that I mentioned I write the following syntax (this Perl command exists in my ksh script)     perl -pe 'next if /^#/; s/(\Q|[[:^alnum:]])$ENV{OLD_TEXT}(\E|[[:^alnum:]])/$1$ENV{NEW_TEXT}$2/g' fileBut this perl command not does the replacement as example 2 !!!The following Example described what I get from my Solaris/linux machine:    export OLD_TEXT='$$OLD_WORD$$'    export NEW_TEXT='&&NEW_WORD&&'     perl -pe 'next if /^#/; s/(\Q|[[:^alnum:]])$ENV{OLD_TEXT}(\E|[[:^alnum:]])/$1$ENV{NEW_TEXT}$2/g' file      &&NEW_WORD&&&&NEW_WORD&&&&NEW_WORD&&&&NEW_WORD&&&&NEW_WORD&&                  &&NEW_WORD&&ABC$&&NEW_WORD&&$&&NEW_WORD&&OLD_&&NEW_WORD&&WORD$&&NC          &&NEW_WORD&&&&NEW_WORD&&&&NEW_WORD&&&&NEW_WORD&&&&NEW_WORD&&&&NEW_WORD&&    &&NEW_WORD&&$&&NEW_WORD&&$&&NEW_WORD&&OLD_&&NEW_WORD&&      &&NEW_WORD&&&&NEW_WORD&&&&NEW_WORD&&&&NEW_WORD&&&&NEW_WORD&&&&NEW_WORD&&     &&NEW_WORD&&1$&&NEW_WORD&&$&&NEW_WORD&&OLD_&&NEW_WORD&Please advice what I need to fix in my perl syntax in order to get  results as  example 2?.
Linux + replace STRING/WORD in file according to rule
linux;shell script;sed;perl;ksh
You need to use negative look-behind/-ahead:export OLD_TEXT='$$OLD_WORD$$'export NEW_TEXT='&&NEW_WORD&&'perl -pe 's/(?<![[:alnum:]])\Q$ENV{OLD_TEXT}\E(?![[:alnum:]])/$ENV{NEW_TEXT}/g' filegives ABC$$OLD_WORD$$ABC&&NEW_WORD&&&&NEW_WORD&&1$$OLD_WORD$$