【问题标题】:How to comment out a string in xml file in shell如何在shell中注释掉xml文件中的字符串
【发布时间】:2018-07-18 04:43:32
【问题描述】:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE sailpoint PUBLIC 'sailpoint.dtd' 'sailpoint.dtd'>
<sailpoint>
<ImportAction name='include' value='custom/Application/CARD/CommonCardFiles/COF Imaging Applications Custom Object.xml'/>
<ImportAction name='include' value='custom/Configuration/COF - Singlenter code heree Entitlement Application Custom Object.xml'/>
<ImportAction name='include' value='custom/Rule/COF Related Application Library.xml'/>
<ImportAction name='include' value='custom/Form/COF LCM CBT Form.xml'/>
<ImportAction name='include' value='custom/Workflow/COF Identity Refresh Bypass.xml'/>
<ImportAction name='include' value='custom/Configuration/COF Unstable Roles Config.xml'/>
<ImportAction name='include' value='custom/Application/CARD/CommonCardFiles/COF Salesforce Common Correlation Rule.xml'/>
<ImportAction name='include' value='custom/Rule/COF Single SOD Violation Detection Rule.xml'/>
<ImportAction name='include' value='custom/Configuration/COF APPLICATION DEPENDENCY 
</sailpoint>*

在上面提到的代码中,我试图用其中的模式自定义/应用程序注释掉所有行

我使用的 sed 命令不起作用

sed '/<custom&Application>/s/\(.*\)/<--\1-->/' est.xml > new.xml

【问题讨论】:

  • 要评论包含value='custom/Application的标签吗?
  • Yes value='custom/Application* 每次出现这种模式。

标签: xml linux shell unix sed


【解决方案1】:

这应该可行

sed -e '/custom\/Application/s/\(^.*$\)/&lt;!--\1--&gt;/' est.xml &gt; new.xml

【讨论】:

    【解决方案2】:

    您可以将完整的行与其中的customApplicatioin 匹配。 匹配字符串使用&amp;

    sed 's/.*custom.*Application.*/<!--&-->/' est.xml > new.xml
    

    【讨论】:

      【解决方案3】:

      应使用 XML 解析器解析 XML 数据。

      您可以删除带有的标签

      xmlstarlet ed -d '//ImportAction[contains(@value,"custom/Application")]' file.xml
      

      我不知道如何评论它们。

      【讨论】:

      • 其实这比评论一行的sed解决方案要好。 xml 元素通常不绑定到一行,sed 解决方案可能会破坏元素中间的内容。
      猜你喜欢
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 2012-01-24
      • 2023-03-12
      • 2011-02-14
      相关资源
      最近更新 更多