复杂 (?) Unix 文本替换命令
命令行相当于什么: 对于内容中包含“AAA”的每个文件,找到“BBB”并将其替换为“CCC”
因此,该命令将匹配并替换文件中的 BBB:
<html>
<head></head>
<body>
AAA
Hello world!
BBB
</body>
</html>
但不在文件中:
<html>
<head></head>
<body>
Don't match me!
BBB
</body>
</html>
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
类似于 grep -l AAA 文件名 | xargs sed -i .bak 's/BBB/CCC/g' 应该可以工作。将来,您可能想在 https://serverfault.com/ 上提出类似的问题。
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.使用正向后看也可以实现
s/(?<=AAA).*?(BBB)/CCC/g
。Using positive look behind also can do the trick
s/(?<=AAA).*?(BBB)/CCC/g
.