我正在使用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>
任何帮助??
答案 0 :(得分: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>
答案 1 :(得分:0)
您的问题有几个问题:
Time
属性,并且未关闭最后一个<Integer>
标记。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示例。