Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

My target is to replace any word/string/Any characters in file with new word/string according to the following rules:

  1. If numeric characters on the left side of word/string then we not replace the word
  2. If numeric characters on the right side of word/string then we not replace the word

remark - 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' file

But 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?

.

share|improve this question
    
OLD_TEXT="$$OLD_WORD$$"; won't work as you expect in shell cause $$ will be replaced with pid. use usual quotes ' instead of double " : OLD_TEXT='$$OLD_WORD$$';. –  rush Dec 11 '12 at 10:12
    
No should be work see the syntax in my perl command "\Q" "\E" they should ignore from this characters –  yael Dec 11 '12 at 10:43
    
It happens before perl. Just try this two commands: export OLD_TEXT="$$OLD_WORD$$" ; echo $OLD_TEXT and export OLD_TEXT='$$OLD_WORD$$' ; echo $OLD_TEXT and try to find some differences. –  rush Dec 11 '12 at 10:47
    
see my update in my queastion –  yael Dec 11 '12 at 11:00
1  
Could you explain/fix your example 2? It is for me not logical why the 2nd $$OLD_WORD$$ is not replaced. –  jofel Dec 11 '12 at 14:52

1 Answer 1

up vote 0 down vote accepted

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' file

gives

ABC$$OLD_WORD$$ABC
&&NEW_WORD&&
&&NEW_WORD&&
1$$OLD_WORD$$
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.