如何用awk或sed递归查找/替换字符串? [英] How to do a recursive find/replace of a string with awk or sed?

查看:172
本文介绍了如何用awk或sed递归查找/替换字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何查找和替换每次出现的情况:

How do I find and replace every occurrence of:

subdomainA.example.com

subdomainB.example.com

是否递归地位于/home/www/目录树下的每个文本文件中?

in every text file under the /home/www/ directory tree recursively?

推荐答案

find /home/www \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i 's/subdomainA\.example\.com/subdomainB.example.com/g'

-print0告诉find打印用空字符而不是换行符分隔的每个结果.如果您的目录中的文件名中带有换行符,这种情况极有可能发生,这仍然可以使xargs使用正确的文件名.

-print0 tells find to print each of the results separated by a null character, rather than a new line. In the unlikely event that your directory has files with newlines in the names, this still lets xargs work on the correct filenames.

\( -type d -name .git -prune \)是一个表达式,它完全跳过名为.git的所有目录.如果使用SVN或要保留其他文件夹,则可以轻松扩展它-只需与更多名称匹配即可.它大致等效于-not -path .git,但是效率更高,因为它不会检查目录中的每个文件,而是会完全跳过它.由于-prune的实际工作方式,因此必须加上-o.

\( -type d -name .git -prune \) is an expression which completely skips over all directories named .git. You could easily expand it, if you use SVN or have other folders you want to preserve -- just match against more names. It's roughly equivalent to -not -path .git, but more efficient, because rather than checking every file in the directory, it skips it entirely. The -o after it is required because of how -prune actually works.

有关更多信息,请参见man find.

For more information, see man find.

这篇关于如何用awk或sed递归查找/替换字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆