Bash,删除空的 XML 标签 [英] Bash, Remove empty XML tags

查看:25
本文介绍了Bash,删除空的 XML 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助,有几个问题,使用 bash 工具

I need some help a couple of questions, using bash tools

  1. 我想从文件中删除空的 xml 标签,例如:

 <CreateOfficeCode>
      <OperatorId>ve</OperatorId>
      <OfficeCode>1234</OfficeCode>
      <CountryCodeLength>0</CountryCodeLength>
      <AreaCodeLength>3</AreaCodeLength>
      <Attributes></Attributes>
      <ChargeArea></ChargeArea>
 </CreateOfficeCode>

变成:

 <CreateOfficeCode>
      <OperatorId>ve</OperatorId>
      <OfficeCode>1234</OfficeCode>
      <CountryCodeLength>0</CountryCodeLength>
      <AreaCodeLength>3</AreaCodeLength>
 </CreateOfficeCode>

为此,我已通过此命令这样做

for this I have done so by this command

sed -i '/><//d' file

这不是那么严格,它更像是一个技巧,更合适的方法是找到 并将其删除.建议?

which is not so strict, its more like a trick, something more appropriate would be to find the <pattern></pattern> and remove it. Suggestion?

  1. 二、怎么走:

 <CreateOfficeGroup>
       <CreateOfficeName>John</CreateOfficeName>
       <CreateOfficeCode>
       </CreateOfficeCode>
 </CreateOfficeGroup>

到:

 <CreateOfficeGroup>
       <CreateOfficeName>John</CreateOfficeName>
 </CreateOfficeGroup>

  1. 作为一个整体?来自:

 <CreateOfficeGroup>
       <CreateOfficeName>John</CreateOfficeName>
       <CreateOfficeCode>
            <OperatorId>ve</OperatorId>
            <OfficeCode>1234</OfficeCode>
            <CountryCodeLength>0</CountryCodeLength>
            <AreaCodeLength>3</AreaCodeLength>
            <Attributes></Attributes>
            <ChargeArea></ChargeArea>
       </CreateOfficeCode>
       <CreateOfficeSize>
            <Chairs></Chairs>
            <Tables></Tables>
       </CreateOfficeSize>
 </CreateOfficeGroup>

到:

 <CreateOfficeGroup>
       <CreateOfficeName>John</CreateOfficeName>
       <CreateOfficeCode>
            <OperatorId>ve</OperatorId>
            <OfficeCode>1234</OfficeCode>
            <CountryCodeLength>0</CountryCodeLength>
            <AreaCodeLength>3</AreaCodeLength>
       </CreateOfficeCode>
 </CreateOfficeGroup>

您能以个人身份回答问题吗?非常感谢!

Can you answer the questions as individuals? Thank you very much!

推荐答案

sed '#n
1h;1!H
$ { x
:remtag
  s#(
* *)*<([^>]*>)( *
*)*</2##g
  t remtag

  p
  }' YourFile

(GNU sed 上的 posix 版本所以 --posix)

(posix version so --posix on GNU sed)

  • 递归地将空标签从下拉杆移至上拉杆,直到不再出现空标签.
  • 不是 XML 解析器,所以类似于 <tag1 prop="<tag2></tag2>">... 也将删除 prop 内容以及 xml 允许的任何其他内容.
  • recursively remove empty tag from lower lever to upper one until no more empty tag occur.
  • Not a XML parser so something like <tag1 prop="<tag2></tag2>"> ... will remove the prop content also and any other thing like that that xml allow.

这篇关于Bash,删除空的 XML 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆