【问题标题】:delete lines between two matches according third inner pattern using sed使用 sed 根据第三个内部模式删除两个匹配项之间的行
【发布时间】:2016-11-29 17:59:07
【问题描述】:

给定:

<tagA/>
<tagB>...</tagB>
<setOfFoo>
  <foo>...</foo>
  <foo>
    ...some special content...
  </foo>
  <foo>...</foo>
  <foo>...</foo>
</setOfFoo>
<tagN/>

我想删除 foo 的整个节点,只有当它有一些特定的内容时,我正在尝试类似:

sed '/<foo>/,/special content/d;/special content/,/<\/foo>/d'
# or
sed '/<foo>/,/<\/foo>/{1,/special content/d;/special content/,$d}'
# or
sed '/<foo>/,/<\/foo>/{:a;N;ba;/special content/d}'

但这些都不起作用,有什么想法吗?

记住:其他foo标签不应该被删除。

【问题讨论】:

  • 使用 XML/HTML 解析器(xmllint、xmlstarlet ...)。

标签: regex xml text replace sed


【解决方案1】:

当我尝试更多时,我找到了解决方案:

sed '/<foo>/,/special content/N;/special content/,/<\/foo>/d'

我仍然发布问题以帮助社区。​​p>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    相关资源
    最近更新 更多