0

我正在使用 Shell 脚本

Config.xml我在文件中有以下内容

<Example>
    <Parameter Name="hello" Time="hello" Time="hello" Conf="1" example="0" Attribute="hello">
    MO
    </Parameter>
    <Parameter Name="hello" Time="hello" Time="hello" Conf="0" example="1" Attribute="hello">
    hiaaa
    </Parameter>
    <Parameter Name="hello" Time="hello" Time="hello" Conf="1" example="1" Attribute="hello">
    youhoo
    </Parameter>
    <Integer Name="hello" Time="hello" Time="hello" Conf="0" example="1" Attribute="hello">
    10
    </Integer>
    <Parameter Name="hello" Time="hello" Time="hello" Conf="1" example="0" Attribute="hello">
    Receive
    </Parameter>
    <Parameter Name="hello" Time="hello" Time="hello" Conf="1" example="0" Attribute="hello">
    reporttt
    </Parameter>
    <Integer Name="hello" Time="hello" Time="hello" Conf="1" example="1" Attribute="hello">
    10
    </Integer>
    <Integer Name="hello" Time="hello" Time="hello" Conf="1" example="1" Attribute="hello">
</Example>

我运行以下 shell 代码来摆脱Name属性

awk '{ {if ($2 ~ "^Name*") $2=""}; print $0}' Config.xml > myConfig.xml

我确实摆脱了Name属性,但得到了以下输出

<Example>
<Parameter  Time="hello" Time="hello" Conf="1" example="0" Attribute="hello">
    MO
    </Parameter>
<Parameter  Time="hello" Time="hello" Conf="0" example="1" Attribute="hello">
    hiaaa
    </Parameter>
<Parameter  Time="hello" Time="hello" Conf="1" example="1" Attribute="hello">
    youhoo
    </Parameter>
<Integer  Time="hello" Time="hello" Conf="0" example="1" Attribute="hello">
    10
    </Integer>
<Parameter  Time="hello" Time="hello" Conf="1" example="0" Attribute="hello">
    Receive
    </Parameter>
<Parameter  Time="hello" Time="hello" Conf="1" example="0" Attribute="hello">
    reporttt
    </Parameter>
<Integer  Time="hello" Time="hello" Conf="1" example="1" Attribute="hello">
    10
    </Integer>
<Integer  Time="hello" Time="hello" Conf="1" example="1" Attribute="hello">
</Example>

我希望输出是这个

<Example>
    <Parameter Time="hello" Time="hello" Conf="1" example="0" Attribute="hello">
    MO
    </Parameter>
    <Parameter Time="hello" Time="hello" Conf="0" example="1" Attribute="hello">
    hiaaa
    </Parameter>
    <Parameter Time="hello" Time="hello" Conf="1" example="1" Attribute="hello">
    youhoo
    </Parameter>
    <Integer Time="hello" Time="hello" Conf="0" example="1" Attribute="hello">
    10
    </Integer>
    <Parameter Time="hello" Time="hello" Conf="1" example="0" Attribute="hello">
    Receive
    </Parameter>
    <Parameter Time="hello" Time="hello" Conf="1" example="0" Attribute="hello">
    reporttt
    </Parameter>
    <Integer Time="hello" Time="hello" Conf="1" example="1" Attribute="hello">
    10
    </Integer>
    <Integer Time="hello" Time="hello" Conf="1" example="1" Attribute="hello">
</Example>

有什么帮助吗??

4

2 回答 2

1

awk与您一起使用gsub可以在不调整间距的情况下删除您喜欢的内容。

awk '$2~/^Name/ {gsub(/ Name="[^"]*"/,x)}1' Config.xml > myConfig.xml
<Example>
    <Parameter Time="hello" Time="hello" Conf="1" example="0" Attribute="hello">
    MO
    </Parameter>
    <Parameter Time="hello" Time="hello" Conf="0" example="1" Attribute="hello">
    hiaaa
    </Parameter>
    <Parameter Time="hello" Time="hello" Conf="1" example="1" Attribute="hello">
    youhoo
    </Parameter>
    <Integer Time="hello" Time="hello" Conf="0" example="1" Attribute="hello">
    10
    </Integer>
    <Parameter Time="hello" Time="hello" Conf="1" example="0" Attribute="hello">
    Receive
    </Parameter>
    <Parameter Time="hello" Time="hello" Conf="1" example="0" Attribute="hello">
    reporttt
    </Parameter>
    <Integer Time="hello" Time="hello" Conf="1" example="1" Attribute="hello">
    10
    </Integer>
    <Integer Time="hello" Time="hello" Conf="1" example="1" Attribute="hello">
</Example>
于 2013-10-30T21:35:20.730 回答
0

你的问题有几个问题:

  • 您的 xml 格式不正确。您为Time每个元素定义了两次属性,并且没有关闭最后一个<Integer>标签。
  • 使用正则表达式解析或修改 xml 并不是一个好主意。一个稳定的解决方案是使用XSLT

remove_name.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!--empty template suppresses this attribute-->
    <xsl:template match="@Name" />
    <!--identity template copies everything forward by default-->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

假设您修复了我在其中命名的错误,config.xml那么您现在可以调用:

xsltproc remove.name.xsl config.xml

删除Name属性。


感谢@MadsHandsen的 xsl 示例。

于 2013-10-30T21:35:10.937 回答