我如何查找并替换每次出现的:

subdomainA.example.com

具有

subdomainB.example.com

递归地在/home/www/目录树下的每个文本文件中?


当前回答

我只使用上衣:

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>)" 

其他回答

如果您想在不完全破坏SVN存储库的情况下使用此功能,可以通过以下操作告诉“查找”忽略所有隐藏文件:

find . \( ! -regex '.*/\..*' \) -type f -print0 | xargs -0 sed -i 's/subdomainA.example.com/subdomainB.example.com/g'

试试看:

sed -i 's/subdomainA/subdomainB/g' `grep -ril 'subdomainA' *`

这是我为OSX和Windows(msys2)找到的最好的全方位解决方案。应该可以使用任何可以获得gnu版本sed的东西。跳过.git目录,这样不会损坏您的校验和。

在mac上,只需先安装coreutils并确保gsed在路径中-

brew install coreutils

然后我将此函数粘贴到zshrc/bashrc->

replace-recursive() {
    hash gsed 2>/dev/null && local SED_CMD="gsed" || SED_CMD="sed"
    find . -type f -name "*.*" -not -path "*/.git/*" -print0 | xargs -0 $SED_CMD -i "s/$1/$2/g"
}

usage: replace-recursive <find> <replace>

用更简单的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 <mydir> -type f -exec sed -i 's/<string1>/<string2>/g' {} +

find<mydir>:在目录中查找。-类型f:文件类型:常规文件-exec命令{}+:-exec操作的这个变体在选定的文件上运行指定的命令,但命令行是通过附加结尾处的每个选定文件名;命令的调用总数将大大少于匹配的文件。命令行的构建方式与xargs构建命令行的方式大致相同。只有一个实例`命令中允许使用“{}”。该命令在起始目录中执行。