且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Bash,删除空的 XML 标签

更新时间:2023-12-05 22:10:22

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.