【问题标题】:Escaping special characters of an xpath in sed在 sed 中转义 xpath 的特殊字符
【发布时间】:2017-08-11 20:37:18
【问题描述】:

我一直在尝试使用 sed 输入一个包含多个特殊字符的值。样本值为//*[@id='login_username']

如果我们使用这个值而不使用任何转义字符,命令将如下所示:

sed -i 's/Text to be replaced/new text and some xpath like //*[@id='login_username']/' target.txt

我尝试使用 \ 字符转义每个特殊字符,结果如下结构://\*\[@id='login_username'\]

还尝试用双引号将值括起来,但这也不起作用。我还能尝试什么?

【问题讨论】:

    标签: bash xpath sed


    【解决方案1】:

    您可以在s/// 命令中使用不同的分隔符来代替/,例如s|||

    sed -i "s|Text to be replaced|new text and some xpath like //*[@id='login_username']|" target.txt
    

    在替换文本中,您唯一需要转义的内容是:

    • " 因为它是用来包围sed 参数的
    • | 因为它被用作s/// 命令中的分隔符

    您可以使用\ 转义这些,例如\"\|

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 2017-03-06
      相关资源
      最近更新 更多