我如何查找并替换每次出现的:
subdomainA.example.com
具有
subdomainB.example.com
递归地在/home/www/目录树下的每个文本文件中?
我如何查找并替换每次出现的:
subdomainA.example.com
具有
subdomainB.example.com
递归地在/home/www/目录树下的每个文本文件中?
当前回答
将当前目录和子目录(不包括.git/)中所有.c和.h文件的所有匹配string_1的内容替换为string_2。
这适用于Mac:
find . -type f -path "*.git*" -prune -o -name '*\.[ch]' -exec \
sed -i '' -e 's/'$1'/'$2'/g' {} +
这应该可以在Linux上运行(尚未测试):
find . -type f -path "*.git*" -prune -o -name '*\.[ch]' -exec \
sed -i 's/string_1/string_2/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' {} \;
注意:不要在包含git repo的文件夹上运行此命令-对.git的更改可能会损坏git索引。
find /home/www/ -type f -exec \
sed -i 's/subdomainA\.example\.com/subdomainB.example.com/g' {} +
与这里的其他答案相比,这比大多数答案都简单,并且使用sed而不是perl,这就是最初的问题所要求的。
perl -p -i -e 's/oldthing/new_thingy/g' `grep -ril oldthing *`
对我来说,最容易记住的解决方案是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解决方案。
用更简单的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
$