我如何查找并替换每次出现的:
subdomainA.example.com
具有
subdomainB.example.com
递归地在/home/www/目录树下的每个文本文件中?
我如何查找并替换每次出现的:
subdomainA.example.com
具有
subdomainB.example.com
递归地在/home/www/目录树下的每个文本文件中?
当前回答
对我来说,最容易记住的解决方案是https://stackoverflow.com/a/2113224/565525即:
sed -i '' -e 's/subdomainA/subdomainB/g' $(find /home/www/ -type f)
注意:-i“”解决了OSX问题sed:1:“…”:命令代码无效。
注意:如果要处理的文件太多,参数列表会太长。解决方法-使用上述find-exec或xargs解决方案。
其他回答
如果您可以访问节点,可以执行npm安装-grexreplace,然后
rexreplace 'subdomainA.example.com' 'subdomainB.example.com' /home/www/**/*.*
有点陈旧,但这在OS X上有效。
有几个诡计:
•仅编辑当前目录下扩展名为.sls的文件
• . 必须转义,以确保sed不会将它们求值为“任何字符”
•,用作sed分隔符,而不是通常的分隔符/
还要注意,这是为了编辑Jinja模板以在导入路径中传递变量(但这是离题的)。
首先,验证sed命令是否符合您的要求(这只会将更改打印到stdout,不会更改文件):
for file in $(find . -name *.sls -type f); do echo -e "\n$file: "; sed 's,foo\.bar,foo/bar/\"+baz+\"/,g' $file; done
准备好进行更改后,根据需要编辑sed命令:
for file in $(find . -name *.sls -type f); do echo -e "\n$file: "; sed -i '' 's,foo\.bar,foo/bar/\"+baz+\"/,g' $file; done
请注意sed命令中的-i“”,我不想创建原始文件的备份(如OS X上使用sed进行的就地编辑或本页中Robert Lujo的评论所述)。
快乐的色丁们!
cd /home/www && find . -type f -print0 |
xargs -0 perl -i.bak -pe 's/subdomainA\.example\.com/subdomainB.example.com/g'
如果您想在不完全破坏SVN存储库的情况下使用此功能,可以通过以下操作告诉“查找”忽略所有隐藏文件:
find . \( ! -regex '.*/\..*' \) -type f -print0 | xargs -0 sed -i 's/subdomainA.example.com/subdomainB.example.com/g'
我很惊讶我没有看到使用文件globbing的简单答案,我只使用**/package.json扫描/更新package.json文件
这是zsh下macos特有的
cd /home/www
sed -i '' -e 's/subdomainA.example.com/subdomainA.example.com/g' **/*