使用sed命令取消对xml块的注释

使用sed命令取消对xml块的注释,xml,bash,sed,Xml,Bash,Sed,我有一个xml文件,其中有许多元素被注释。从所有这些元素中,我想使用sed命令取消注释一个元素 我的xml文件为: <!-- This is the sample xml which holds the data of the students --> <Students> <!-- <student> <name>john</> <id>123</id>

我有一个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命令,但没有得到如何从最后一个块中删除just和->的命令。是否可以用as NewName取消对xml块的注释?除了删除整行之外,我什么也没找到

编辑:我可以有许多xml元素,而不是像、、。

不要使用sed。使用

在命令行上:

xsltproc -o output.xml uncomment.xsl input.xml 如果它工作正常,您的输入XML将得到以下结果:

<!-- This is the sample xml
    which holds the data of the students -->
<Students>
    <student>
        <name>john</name>
        <id>123</id>
    </student>
    <student>
        <name>mike</name>
        <id>234</id>
    </student>
    <student>
        <name>NewName</name>
        <id>NewID</id>
    </student>
</Students>

这可能适用于GNU sed:

sed -r '/<Students>/,/<\/Students>/{/<Students>/{h;d};H;/<\/Students>/!d;g;s/(.*)<!-- (.*) -->(.*)/\1\2\3/}' file

这将学生数据存储在保留空间中,然后使用GRADE查找上次出现的和->并在打印数据之前将其删除。

在您的输入中,只有第一个学生被注释包围。第三个不是。事实上,输入不是有效的XML。我已经更新了问题。我打错了。但是问题仍然存在,我怀疑你想做的是可能的。解析XML很难。那么还有其他方法使用awk或其他什么吗?但我只想使用shell脚本,我只想删除最后一个名为NewName的student元素。在这里,student是父标记,student是子元素。所以我对你的命令感到困惑matching@Optimus这将存储多行,然后删除最后的开始/结束注释标记。谢谢@potong,请告诉我为什么需要\1\2\3?@Optimus这些是。
sed -r '/<Students>/,/<\/Students>/{/<Students>/{h;d};H;/<\/Students>/!d;g;s/(.*)<!-- (.*) -->(.*)/\1\2\3/}' file