使用 xargs sed 查找和替换 - 我可以将结果保存到文件中吗?
我的代码将 'foo' 的所有实例替换为 'bar' :
find . -type f |
xargs grep 'foo' -l |
xargs sed -i 's|foo|bar|g'
我想将修改后的文件列表保存到文本文档中。是否可以?
编辑:
这是对我有用的最终代码:
find . -type f -print0 |
xargs -0 grep 'foo' -l |
tee result.txt |
xargs -0 sed -i 's|foo|bar|g'
不确定这是否是最快的方法,但对于几千个文件,此方法与其他建议方法之间的速度差异可能非常小。
My code to replace all instances of 'foo' with 'bar' :
find . -type f |
xargs grep 'foo' -l |
xargs sed -i 's|foo|bar|g'
I'd like to save a list of the modified files to a text document. Is it possible?
EDIT :
This is the final code that worked for me :
find . -type f -print0 |
xargs -0 grep 'foo' -l |
tee result.txt |
xargs -0 sed -i 's|foo|bar|g'
Not sure whether this is the quickest way, but for a few thousand files the difference in speed between this and other suggested methods is probably very small.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来 xargs 的使用毫无用处,就像经常与 find 结合使用一样。
请小心使用,因为我没有测试过。我不确定您是否想更改文件名列表或文件内容。由于您使用
grep
和sed
进行搜索,我认为仅使用sed
就足够了:Looks like a useless use of
xargs
, as often in combination withfind
.Use it with care, since I didn't test it. I'm not sure, whether you like to change the list of filenames, or the file content. Since you search with
grep
andsed
, I think only working withsed
should be sufficient: