Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Example1 work fine when I want to replace OLD_TEXT with NEW_TEXT ( its replace also all special characters as $@^%)(_+`:; etc ..)


  export OLD_TEXT='$$OLD_WORD$$'
  export NEW_TEXT='$$NEW_WORD$$'

  perl -pe 'next if /^#/; s/\Q$ENV{OLD_TEXT}\E/$1$ENV{NEW_TEXT}$2/g' file 

on example2

I add the "[[:^alnum:]]" in the perl syntax in order to ignore TEXT that start with alpha numeric characters or end with alpha numric characters

but as all see it doesn't work why?

remark: alpha numric characters means numbers OR [a-z][A-Z]

Real Example

 #example2 on LINUX machine:

  more file



   perl -pe 'next if /^#/; s/(\Q|[[:^alnum:]])$ENV{OLD_TEXT}(\E|[[:^alnum:]])/$1$ENV{NEW_TEXT}$2/g' file



Example what I should get:

  more file

share|improve this question

migrated from serverfault.com Dec 11 '12 at 17:25

This question came from our site for system and network administrators.

You want [^[:alnum:]], not [[:^alnum:]]. [:alnum:] is the POSIX character class being referred to. The brackets surrounding the brackets of that character class are the scope that you want to be within.

share|improve this answer
hi Chris - I change it but still its not replace the word : perl -pe 'next if /^#/; s/(\Q|[^[:alnum:]])$ENV{OLD_TEXT}(\E|[^[:alnum:]])/$1$ENV{NEW_TEXT}$2/g' file – yael Dec 16 '12 at 9:34

Your Answer


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