Linux Bash-在多个目录中查找并替换文件中的一行
我有一个文件结构,其中有700个目录。所有目录都有一个名为Linux Bash-在多个目录中查找并替换文件中的一行,linux,bash,shell,Linux,Bash,Shell,我有一个文件结构,其中有700个目录。所有目录都有一个名为config.xml的文件。我用1111-name作为25个目录的前缀。现在,我想替换所有25个目录中config.xml文件中的一块代码。我希望有一个循环(用于或while)用于该循环。我将使用递归函数: folder_recursive() { for i in "$1"/*;do if [ -d "$i" ];then folder_recursive "$i" eli
config.xml
的文件。我用1111-
name作为25个目录的前缀。现在,我想替换所有25个目录中config.xml
文件中的一块代码。我希望有一个循环(用于
或while
)用于该循环。我将使用递归函数:
folder_recursive() {
for i in "$1"/*;do
if [ -d "$i" ];then
folder_recursive "$i"
elif [ -f "$i" ]; then
lastFolder = ${$1##*/}
if [[ $lastFolder == *"1111-"* ]]; then
if [[ $i == *"config.xml" ]]; then
#Replace block of code
file_path=$i
original=$(<file_path) # Read from file
to_replace="aaaabbbb"
replacer="xxxxyyyy"
result=${original/$to_replace/$replacer} #This will replace the first instance of to_replace
# If you want to replace all instances use:
# ${original//$to_replace/$replacer}
echo $result > $i #Overwrite file
fi
fi
fi
done
}
path= "root/of/folder/structure"
folder_recursive $path
folder\u recursive(){
对于“$1”/*”中的i;请
如果[-d“$i”];则
文件夹\u递归“$i”
elif[-f“$i”];然后
lastFolder=${$1##*/}
如果[[$lastFolder==*“1111-”*];则
如果[[$i==*“config.xml”]];则
#替换代码块
文件路径=$i
原始=$($i#覆盖文件
fi
fi
fi
完成
}
path=“root/of/folder/structure”
文件夹\u递归$path
我将使用递归函数:
folder_recursive() {
for i in "$1"/*;do
if [ -d "$i" ];then
folder_recursive "$i"
elif [ -f "$i" ]; then
lastFolder = ${$1##*/}
if [[ $lastFolder == *"1111-"* ]]; then
if [[ $i == *"config.xml" ]]; then
#Replace block of code
file_path=$i
original=$(<file_path) # Read from file
to_replace="aaaabbbb"
replacer="xxxxyyyy"
result=${original/$to_replace/$replacer} #This will replace the first instance of to_replace
# If you want to replace all instances use:
# ${original//$to_replace/$replacer}
echo $result > $i #Overwrite file
fi
fi
fi
done
}
path= "root/of/folder/structure"
folder_recursive $path
folder\u recursive(){
对于“$1”/*”中的i;请
如果[-d“$i”];则
文件夹\u递归“$i”
elif[-f“$i”];然后
lastFolder=${$1##*/}
如果[[$lastFolder==*“1111-”*];则
如果[[$i==*“config.xml”]];则
#替换代码块
文件路径=$i
原始=$($i#覆盖文件
fi
fi
fi
完成
}
path=“root/of/folder/structure”
文件夹\u递归$path
您可以使用查找,grep
目录搜索文件,并使用sed
替换字符串
find . -iname 'config.xml' | grep '1111' | sed -i 's/STRINGTOREPLACE/NEWSTRING/'
您可以使用find
,grep
目录搜索文件,并使用sed
替换字符串
find . -iname 'config.xml' | grep '1111' | sed -i 's/STRINGTOREPLACE/NEWSTRING/'
试试这个:
find . -name "1111-*" -type d -exec cd {} \; -exec /absolute.path/to/shell_script_modifying_config.xml \;
试试这个:
find . -name "1111-*" -type d -exec cd {} \; -exec /absolute.path/to/shell_script_modifying_config.xml \;
到目前为止写了什么?到目前为止写了什么?这是一个递归函数吗?@Luca:我不确定这将如何替换代码。我将告诉这个函数在哪里查找特定的行然后用新代码替换它!!请hellp!@Drew这段代码没有完成你提到的部分。它递归地搜索文件夹结构,找到你要编辑的文件。你没有给出如何编辑它们的详细信息,所以我插入了注释#替换代码块。@luca:假设我有一个字符串“aaaabbbbb”在所有config.xml文件中,我希望将其替换为“xxxxyyyy”。现在你将如何重新编写该函数。抱歉,我是一个新手。我正在用示例代码编辑答案。让我知道它是否有效这是一个递归函数吗?@Luca:我不确定这将如何替换代码。我将告诉该函数在哪里查找特定行然后用新代码替换它!!请hellp!@Drew这段代码没有完成你提到的部分。它递归地搜索文件夹结构,找到你要编辑的文件。你没有给出如何编辑它们的详细信息,所以我插入了注释#替换代码块。@luca:假设我有一个字符串“aaaabbbbb”在所有config.xml文件中,我希望将其替换为“xxxxyyyy”。现在你将如何重新编写该函数。抱歉,我是一个新手。我正在用示例代码编辑答案。请告诉我它是否有效。如果文件配置位于1111个文件夹的子目录中,该怎么办。grep也会得到该功能,我想@Drew不需要该功能。嗯。在这种情况下,可能需要使用-depth参数。可能ls 1111-*/config.xml
比find…
@Luca:这正是发生的!!!我在子文件夹中也有config.xml文件。@OhadBasan:我使用了-maxdepth标志,它工作了。但是,在运行命令后,它抱怨没有sed命令的输入文件。有什么帮助吗?如果在子文件夹中找到了配置文件呢其中一个1111文件夹的目录.grep也会得到它,我不认为@Drew想要这个.hmm。在这种情况下,可能需要使用-depth参数。也许ls 1111-*/config.xml
比find…
@Luca:这就是确切发生的事情!!!我在子文件夹中也有config.xml文件。@OhadBasan:我使用了-maxdepth标志,它工作了。但是,在运行命令后,它抱怨没有sed命令的输入文件。有帮助吗?我没有使用grep。我提供的find命令搜索以1111-开头的目录。然后它进入这些目录并运行shell脚本。因此它不会进入1111-文件夹的子目录。我不是我提供的find命令搜索以1111-开头的目录。然后它进入这些目录并运行shell脚本。因此它不会进入1111-文件夹的子目录。