tags:

views:

44

answers:

2

I use the following Perl syntax in my bash script:

     perl -i –pe  'next if /^ *#/; s/(\b|\D)$ENV{OLD }(\b|\D)/$1$ENV{NEW }$2/'   file

I want to find the OLD word without first "#" character in the file , then replaces the OLD word with NEW word

My question: I want to print "replace $OLD with $NEW" each time perl replace the $OLD with $NEW

In which way? we can insert the command: print "replaced $OLD with $NEW "; in the Perl syntax?

Please notice that perl one-liner syntax is part of my bash script

+2  A: 

perl -i –pe 'next if /^ *#/; s/(\b|\D)$ENV{OLD }(\b|\D)/$1$ENV{NEW }$2/ && warn "replaced $OLD with $NEW\n"' file

Sinan Ünür
The -i switch makes the output from `print` go to the current file. I guess this was not desired by the OP
eugene y
You should check the result of the s/// to see if you actually made a substitution. ;)
brian d foy
@eugene Thank you. I knew that ;-) @brian *Sigh* I know, I know. Thank you for the edit. I guess I jumped the gun.
Sinan Ünür
+1  A: 

perl -i –pe 'next if /^ *#/; s/(\b|\D)$ENV{OLD }(\b|\D)/$1$ENV{NEW }$2/ && print STDERR "replaced $OLD with $NEW\n"' file

eugene y