开发者

Complex (?) Unix Text Replace Command

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜