Sed Unix Shell脚本在子文件夹中的特定文件中查找和替换字符串
我只想将文件夹子文件夹中存在的xml文件中的字符串“解决问题”替换为“选择最佳答案”。我已经编译了一个脚本来帮助我完成这项工作,但是有两个问题Sed Unix Shell脚本在子文件夹中的特定文件中查找和替换字符串,sed,Sed,我只想将文件夹子文件夹中存在的xml文件中的字符串“解决问题”替换为“选择最佳答案”。我已经编译了一个脚本来帮助我完成这项工作,但是有两个问题 它还将替换脚本的内容 它将替换子文件夹中所有文件中的文本(但我只希望更改xml) 如果特定子文件夹和文件中出现文本不匹配,我希望显示错误消息(最好是文本输出) 因此,你能帮我修改我现有的脚本,以便我可以解决上述3个问题 我的剧本是: 查找-键入f | xargs sed-i“s/解决问题/选择最佳答案/g” find -type f -name "*.x
find -type f -name "*.xml" | xargs sed -i "s/Solve the problem/Choose the best answer/g"
不确定我是否理解问题3。使用bash和sed:
search='Solve the problem'
replace='Choose the best answer'
for file in `find -name '*.xml'`; do
grep "$search" $file &> /dev/null
if [ $? -ne 0 ]; then
echo "Search string not found in $file!"
else
sed -i "s/$search/$replace/" $file
fi
done