我如何查找并替换每次出现的:
subdomainA.example.com
具有
subdomainB.example.com
递归地在/home/www/目录树下的每个文本文件中?
我如何查找并替换每次出现的:
subdomainA.example.com
具有
subdomainB.example.com
递归地在/home/www/目录树下的每个文本文件中?
当前回答
用更简单的fd(1)/fdfind=替换find(1)https://github.com/sharkdp/fd:
fdfind . --type f --exec sed -i "s/original_string/new_string/g"
寻址fd(1)iconsistent pkg和cmd名称
在macOS自制软件上:pkg和cmd=fd在Ubuntu 20.04上:pkg=fd find,cmd=fdfind
我在macOS上创建了一个别名fdfind='fd',以实现一致的cmd命名(在我的macOS和Linux平台之间)。
有关这一点的更多信息,请访问https://github.com/sharkdp/fd/issues/1009.
更多细节和附加功能
# bash examples:
1='original_string'
2='new______string'
# for this (the original-poster's) question:
1='subdomainA.example.com'
2='subdomainB.example.com'
# 'fdfind' (on at least Ubuntu 20.04) = 'fd' = https://github.com/sharkdp/fd
fdfind . --type f --exec sed -i "s/$1/$2/g"
# Here's a slightly-more-complex example that
# a. excludes (-E) .git/ and archive/ dirs, and
# b. performs a word-boundary search on the original_string (\<$1\>):
fdfind . -E .git/ -E archive/ --type f --exec sed -i "s/\<$1\>/$2/g"
甚至更高级:从第三个($3)命令行参数控制单词边界(第三个参数=noword表示无边界,leftword表示仅左侧单词边界,rightword表示仅右侧边界):
#!/usr/bin/env bash
#
# replace-tree.bash
#
# 'fdfind' (on at least Ubuntu 20.04) = 'fd' = https://github.com/sharkdp/fd
if [ $# -lt 2 ]; then
echo "$0: Please provide at least 2 arguments."
exit 1
fi
original="\<$1\>"
if [ "$3" = "noword" ]; then
original="$1"
elif [ "$3" = "leftword" ]; then
original="\<$1"
elif [ "$3" = "rightword" ]; then
original="$1\>"
fi
fdfind . --type f --exec sed -i "s/$original/$2/g"
示例用法:
$ replace-tree.bash original_string new_string leftword
$
其他回答
find /home/www \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i 's/subdomainA\.example\.com/subdomainB.example.com/g'
-print0告诉find打印由空字符分隔的每个结果,而不是新行。如果您的目录中包含名称中带有换行符的文件,这是不太可能的,那么xargs仍然可以使用正确的文件名。
\(-type d-name.git-prenne\)是一个表达式,它完全跳过名为.git的所有目录。如果您使用SVN或有其他要保留的文件夹,则可以轻松展开它,只需匹配更多名称即可。它大致相当于-not-path.git,但效率更高,因为它不检查目录中的每个文件,而是完全跳过它。后面的-o是必需的,因为-prime实际上是如何工作的。
有关更多信息,请参阅man find。
#!/usr/local/bin/bash -x
find * /home/www -type f | while read files
do
sedtest=$(sed -n '/^/,/$/p' "${files}" | sed -n '/subdomainA/p')
if [ "${sedtest}" ]
then
sed s'/subdomainA/subdomainB/'g "${files}" > "${files}".tmp
mv "${files}".tmp "${files}"
fi
done
对于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
我只是需要这一点,并对现有示例的速度感到不满意。所以我想出了自己的办法:
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个文件,而其他命令耗时太长,我无法等到它们完成。
如果您想在不完全破坏SVN存储库的情况下使用此功能,可以通过以下操作告诉“查找”忽略所有隐藏文件:
find . \( ! -regex '.*/\..*' \) -type f -print0 | xargs -0 sed -i 's/subdomainA.example.com/subdomainB.example.com/g'