我如何查找并替换每次出现的:
subdomainA.example.com
具有
subdomainB.example.com
递归地在/home/www/目录树下的每个文本文件中?
我如何查找并替换每次出现的:
subdomainA.example.com
具有
subdomainB.example.com
递归地在/home/www/目录树下的每个文本文件中?
当前回答
对于任何使用银搜索器(ag)的人
ag SearchString -l0 | xargs -0 sed -i 's/SearchString/Replacement/g'
由于ag默认忽略git/hg/svn文件/文件夹,因此在存储库中运行是安全的。
其他回答
如果您想在不完全破坏SVN存储库的情况下使用此功能,可以通过以下操作告诉“查找”忽略所有隐藏文件:
find . \( ! -regex '.*/\..*' \) -type f -print0 | xargs -0 sed -i 's/subdomainA.example.com/subdomainB.example.com/g'
cd /home/www && find . -type f -print0 |
xargs -0 perl -i.bak -pe 's/subdomainA\.example\.com/subdomainB.example.com/g'
只是为了避免改变
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' {} \;
对于IBMi上的Qshell(qsh),不是OP标记的bash。
qsh命令的限制:
find没有-print0选项xargs没有-0选项sed没有-i选项
因此,qsh中的解决方案:
PATH='your/path/here'
SEARCH=\'subdomainA.example.com\'
REPLACE=\'subdomainB.example.com\'
for file in $( find ${PATH} -P -type f ); do
TEMP_FILE=${file}.${RANDOM}.temp_file
if [ ! -e ${TEMP_FILE} ]; then
touch -C 819 ${TEMP_FILE}
sed -e 's/'$SEARCH'/'$REPLACE'/g' \
< ${file} > ${TEMP_FILE}
mv ${TEMP_FILE} ${file}
fi
done
注意事项:
解决方案不包括错误处理不是OP标记的Bash
根据这篇博文:
find . -type f | xargs perl -pi -e 's/oldtext/newtext/g;'