我如何查找并替换每次出现的:
subdomainA.example.com
具有
subdomainB.example.com
递归地在/home/www/目录树下的每个文本文件中?
我如何查找并替换每次出现的:
subdomainA.example.com
具有
subdomainB.example.com
递归地在/home/www/目录树下的每个文本文件中?
当前回答
我只是需要这一点,并对现有示例的速度感到不满意。所以我想出了自己的办法:
cd /var/www && ack-grep -l --print0 subdomainA.example.com | xargs -0 perl -i.bak -pe 's/subdomainA\.example\.com/subdomainB.example.com/g'
Ack-grep在查找相关文件方面非常有效。这个命令轻而易举地替换了约145000个文件,而其他命令耗时太长,我无法等到它们完成。
其他回答
更简单的方法是在命令行上使用以下命令
find /home/www/ -type f|xargs perl -pi -e 's/subdomainA\.example\.com/subdomainB.example.com/g'
如果您可以访问节点,可以执行npm安装-grexreplace,然后
rexreplace 'subdomainA.example.com' 'subdomainB.example.com' /home/www/**/*.*
要更改多个文件(并将备份保存为*.bak):
perl -p -i -e "s/\|/x/g" *
将获取目录中的所有文件并用x替换|称为“Perl馅饼”(简单如馅饼)
或者使用极快的GNU Parallel:
grep -rl oldtext . | parallel sed -i 's/oldtext/newtext/g' {}
对于任何使用银搜索器(ag)的人
ag SearchString -l0 | xargs -0 sed -i 's/SearchString/Replacement/g'
由于ag默认忽略git/hg/svn文件/文件夹,因此在存储库中运行是安全的。