开发者

Complex (?) Unix Text Replace Command

开发者 https://www.devze.com 2023-02-10 12:32 出处:网络
What\'s the command line equivalent of: For every file that contains \"AAA\" within its contents, find \"BBB\" and replace it with \"CCC\"

What's the command line equivalent of: For every file that contains "AAA" within its contents, find "BBB" and replace it with "CCC"

Thus, the command would match and replace BBB in a file:

<html>
<head></head>
<body>
AAA
Hello world!
BBB    
</body>
</html>

But Not in a开发者_开发技巧 file:

<html>
<head></head>
<body>
Don't match me!
BBB    
</body>
</html>

Thanks in advance!


Something like grep -l AAA file-names | xargs sed -i .bak 's/BBB/CCC/g' should work. In the future, you might want to ask questions like this on https://serverfault.com/ instead.


Using positive look behind also can do the trick s/(?<=AAA).*?(BBB)/CCC/g.

0

精彩评论

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

关注公众号