开发者

How can I print a message for each substitution in my Perl one-liner?

开发者 https://www.devze.com 2023-01-21 00:29 出处:网络
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 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" 开发者_JAVA技巧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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消