开发者

perl -pi -e syntax with | or <

开发者 https://www.devze.com 2023-02-14 22:19 出处:网络
If I want to replace all cases of foo with bar I simply do this. perl -pi -e \'s/foo/bar/gi\' /home/smith/myfile.txt

If I want to replace all cases of foo with bar I simply do this.

perl -pi -e 's/foo/bar/gi' /home/smith/myfile.txt

What if I only wa开发者_开发百科nt to replace |foo| with |bar|?

Related question..What if I want to only replace >foo< with >bar< ?


You have to escape the | character:

perl -pi -e 's/\|foo\|/\|bar\|/gi' /home/smith/myfile.txt


Try:

perl-pi -e 's/\Q|foo|\E/|bar|gi' /home/smith/myfile.txt

See perldoc perlre and search for /Escape sequences/.


You simply need to use proper quoting. With ' quotes you're fairly safe to do what you want. Except for things related to regexp characters, like the '|'. so it would become this:

perl -pi -e 's/\|foo\|/|bar|/gi' /home/smith/myfile.txt

The >foo< examples are easier because they're not regexp characters.

0

精彩评论

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