多文件字符串查找替换

多文件字符串查找替换

1
2
3
4
5
6
7
8
9
find_str=$1                     # 对应当前路径下多文件中要查找的字符串
replace_str=$2                  # 对应当前路径下多文件中要替换的新字符串

mapfile -t files < <(find ./ -name "*.c" -o -name "*.h" -o -name "*.cpp" | xargs grep -ainw "${find_str}" | awk -F ':' '{print $1}') # -name指定了要查找的文件类型
# echo "${files[@]}"              # 输出所有包含要查找的字符串的文件

for ((i = 0; i < "${#files[@]}"; i++)); do
  sed -i "s/${find_str}/${replace_str}/g" "${files[${i}]}" # 进行字符串替换,用法为:sed -i "s/新字符串/旧字符串/g file",由于涉及到变量,因此使用双引号而不是单引号
done

使用示例

1
~/find-replace.sh "text1" "text2"
Licensed under CC BY-NC-SA 4.0
最后更新于 Jul 11, 2025 16:22 +0800
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计