replace - 使用 xargs sed 查找和替换 - 我可以将结果保存到文件中吗?

标签 replace sed find grep xargs

我的代码将“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'

不确定这是否是最快的方法,但对于几千个文件,此方法与其他建议方法之间的速度差异可能非常小。

最佳答案

看起来像是对 xargs 的无用使用,就像经常与 find 结合使用一样。

find . -type f -exec grep 'foo' -l {} ";" -exec sed -i 's|foo|bar|g' {} ";" -ls > file.lst

请小心使用它,因为我没有测试过它。我不确定您是否想更改文件名列表或文件内容。由于您使用 grepsed 进行搜索,我认为仅使用 sed 就足够了:

find . -type f -exec sed -i 's|foo|bar|g' {} ";" -ls > file.lst

关于replace - 使用 xargs sed 查找和替换 - 我可以将结果保存到文件中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9086816/

相关文章:

regex - 如何仅在 powershell 的子字符串中替换特定字符

bash - 将文本附加到文件 bash 中的特定行

linux - 如何删除 3 小时前创建的 linux 目录中的文件

python - 如何将任何 ip 地址转换为斜杠 24 前缀

javascript - 字符串替换表达式

regex - 奇怪的 sed 行为

unix - sed 用条件替换字符串

vba - Excel VBA : Way to find the value of a cell with multiple criteria

matlab - 矢量化 Matlab 'find' 的多个调用

c++ - 搜索特定行 C++ 制表符分隔