我如何查找并替换每次出现的:
subdomainA.example.com
具有
subdomainB.example.com
递归地在/home/www/目录树下的每个文本文件中?
我如何查找并替换每次出现的:
subdomainA.example.com
具有
subdomainB.example.com
递归地在/home/www/目录树下的每个文本文件中?
当前回答
如果您需要排除目录(--exclude-dir=..folder),并且文件名可能带有空格(通过对grep-Z和xargs-0使用0Byte来解决),这是一种直接的方法
grep -rlZ oldtext . --exclude-dir=.folder | xargs -0 sed -i 's/oldtext/newtext/g'
其他回答
对我来说最简单的方法是
grep -rl oldtext . | xargs sed -i 's/oldtext/newtext/g'
试试看:
sed -i 's/subdomainA/subdomainB/g' `grep -ril 'subdomainA' *`
perl -p -i -e 's/oldthing/new_thingy/g' `grep -ril oldthing *`
我很惊讶我没有看到使用文件globbing的简单答案,我只使用**/package.json扫描/更新package.json文件
这是zsh下macos特有的
cd /home/www
sed -i '' -e 's/subdomainA.example.com/subdomainA.example.com/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>)"