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

subdomainA.example.com

具有

subdomainB.example.com

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


当前回答

有点陈旧,但这在OS X上有效。

有几个诡计:

•仅编辑当前目录下扩展名为.sls的文件

• . 必须转义,以确保sed不会将它们求值为“任何字符”

•,用作sed分隔符,而不是通常的分隔符/

还要注意,这是为了编辑Jinja模板以在导入路径中传递变量(但这是离题的)。

首先,验证sed命令是否符合您的要求(这只会将更改打印到stdout,不会更改文件):

for file in $(find . -name *.sls -type f); do echo -e "\n$file: "; sed 's,foo\.bar,foo/bar/\"+baz+\"/,g' $file; done

准备好进行更改后,根据需要编辑sed命令:

for file in $(find . -name *.sls -type f); do echo -e "\n$file: "; sed -i '' 's,foo\.bar,foo/bar/\"+baz+\"/,g' $file; done

请注意sed命令中的-i“”,我不想创建原始文件的备份(如OS X上使用sed进行的就地编辑或本页中Robert Lujo的评论所述)。

快乐的色丁们!

其他回答

如果您可以访问节点,可以执行npm安装-grexreplace,然后

rexreplace 'subdomainA.example.com' 'subdomainB.example.com' /home/www/**/*.*

如果您不介意将vim与grep或find工具一起使用,您可以在以下链接中跟进用户Gert给出的答案-->如何在大文件夹层次结构中进行文本替换?。

交易如下:

递归地对要在某个路径中替换的字符串执行grep,并只获取匹配文件的完整路径。(这将是$(grep”字符串“”路径名“-Rl”)。(可选)如果您想对集中目录上的这些文件进行预备份,您也可以使用以下命令:cp-iv$(grep‘string‘‘pathname‘-Rl)‘集中目录路径名‘之后,您可以在vim中按照与给定链接上提供的方案类似的方案随意编辑/替换::bufdo%s#string#replacement#gc | update

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

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

使用grep和sed的组合

for pp in $(grep -Rl looking_for_string)
do
    sed -i 's/looking_for_string/something_other/g' "${pp}"
done

用更简单的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
$