我如何查找并替换每次出现的:
subdomainA.example.com
具有
subdomainB.example.com
递归地在/home/www/目录树下的每个文本文件中?
我如何查找并替换每次出现的:
subdomainA.example.com
具有
subdomainB.example.com
递归地在/home/www/目录树下的每个文本文件中?
当前回答
这一个与git存储库兼容,而且更简单:
Linux:
git grep -l 'original_text' | xargs sed -i 's/original_text/new_text/g'
Mac:
git grep -l 'original_text' | xargs sed -i '' -e 's/original_text/new_text/g'
(感谢http://blog.jasonmeridth.com/posts/use-git-grep-to-replace-strings-in-files-in-your-git-repository/)
其他回答
最简单的替换方式(所有文件、目录、递归)
find . -type f -not -path '*/\.*' -exec sed -i 's/foo/bar/g' {} +
注意:有时您可能需要忽略一些隐藏文件,例如.git,您可以使用上面的命令。
如果要包含隐藏文件,请使用,
find . -type f -exec sed -i 's/foo/bar/g' {} +
在这两种情况下,字符串foo将被替换为新的字符串栏
只是为了避免改变
NearlysubdomainA.example.com附近子域例如comp.other
但仍然
子域A.example.com.IIt.good
(在域名根背后的想法可能不太好)
find /home/www/ -type f -exec sed -i 's/\bsubdomainA\.example\.com\b/\1subdomainB.example.com\2/g' {} \;
我只使用上衣:
find . -name '*.[c|cc|cp|cpp|m|mm|h]' -print0 | xargs -0 tops -verbose replace "verify_noerr(<b args>)" with "__Verify_noErr(<args>)" \
replace "check(<b args>)" with "__Check(<args>)"
您可以使用awk解决如下问题,
for file in `find /home/www -type f`
do
awk '{gsub(/subdomainA.example.com/,"subdomainB.example.com"); print $0;}' $file > ./tempFile && mv ./tempFile $file;
done
希望这对你有帮助!!!
试试看:
sed -i 's/subdomainA/subdomainB/g' `grep -ril 'subdomainA' *`