awk无缘无故地删除空间

时间:2013-10-30 21:25:54

标签: shell awk

我正在使用Shell Scripting

我在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>

任何帮助??

2 个答案:

答案 0 :(得分:1)

awkgsub一起使用,即可在不调整间距的情况下删除您喜欢的内容。

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>

答案 1 :(得分: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示例。