gpt4 book ai didi

sed - Unix Shell 脚本查找和替换子文件夹中特定文件中的字符串

转载 作者:行者123 更新时间:2023-12-04 22:22:50 29 4
gpt4 key购买 nike

我想仅在文件夹的子文件夹中存在的 xml 文件中将字符串“解决问题”替换为“选择最佳答案”。我已经编译了一个脚本来帮助我做到这一点,但是有两个问题

  • 它还替换了脚本
  • 的内容
  • 它替换子文件夹的所有文件中的文本(但我只想更改 xml)
  • 如果文本不匹配发生在特定的子文件夹和文件中,我想显示错误消息(最好是文本输出)。

  • 那么请您帮我修改我现有的脚本,以便我可以解决上述 3 个问题。

    我的脚本是:

    find -type f | xargs sed -i "s/解决问题/选择最佳答案/g"

    最佳答案

    使用 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

    关于sed - Unix Shell 脚本查找和替换子文件夹中特定文件中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11858426/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com