【发布时间】:2014-06-08 07:31:14
【问题描述】:
我有一个 xml 文件,其中包含许多注释的元素。从所有这些元素中,我想使用 sed 命令取消注释一个元素。
我的 xml 文件为:
<!-- This is the sample xml
which holds the data of the students -->
<Students>
<!-- <student>
<name>john</>
<id>123</id>
</student> -->
<student>
<name>mike</name>
<id>234</id>
</student>
<!-- <student>
<name>NewName</name>
<id>NewID</id>
</student> -->
</Students>
在上面的 xml 文件中,我想取消注释最后一个 xml 块,所以我的文件看起来像
<!-- This is the sample xml
which holds the data of the students -->
<Students>
<!-- <student>
<name>john</>
<id>123</id>
</student> -->
<student>
<name>mike</name>
<id>234</id>
</student>
<student>
<name>NewName</name>
<id>NewID</id>
</student>
</Students>
我执行了 sed 命令,但不知道如何从最后一个块中删除 <!-- 和 -->。是否可以将带有 <name> 的 xml 块取消注释为 NewName ?除了删除整行之外,我没有发现任何东西。
编辑:除了<name> 和<id> 之外,我可以有许多xml 元素,例如<address>, <city>, <class>,<marks>。
【问题讨论】:
-
在您的输入中,只有第一个
student被评论包围。第三个不是。实际上,输入的 XML 无效。 -
我已经更新了这个问题。我错了,我在这里打错了。但问题依然存在
-
我怀疑你想做的事情是可能的。解析 XML 很困难。
-
那么还有其他方法可以使用 awk 什么的吗?但我只想使用 shell 脚本