Shell awk无故删除空间

Shell awk无故删除空间,shell,awk,Shell,Awk,我正在使用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" ex

我正在使用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>
我确实去掉了
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>

卫生官员
你好
友呼
10
接收
报告员
10
我希望输出是这样的

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

卫生官员
你好
友呼
10
接收
报告员
10

有什么帮助吗???

您的问题有几个问题:

  • 您的xml格式不正确。您正在为每个元素定义两次
    Time
    属性,并且没有关闭最后一个
    标记
  • 用正则表达式解析或修改xml不是一个好主意。一个稳定的解决方案是使用
    XSLT
删除\u 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>
要删除
名称
属性



感谢xsl示例。

使用
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>
awk'$2~/^Name/{gsub(/Name=“[^”]*”/,x)}1'Config.xml>myConfig.xml
卫生官员
你好
友呼
10
接收
报告员
10

不要使用awk解析xml。使用DOM或XPath您的主题是“awk无缘无故删除空间”,这意味着AWK正在删除您要保留的空间;但是如果我理解正确的话,问题实际上是AWK并没有删除空格,而您希望它这样做。这是正确的吗?请告诉我你遇到的具体问题。你的问题可以归结为:我知道这个,但我想要那个。有什么帮助吗?这不是一个有用的问题。不要让我们猜出你不喜欢输出的地方——快出来告诉我们。@hek2mgl抱歉,我没有这些软件包,也没有许可install@ruakh不,如果你看到我的期望和我在处决后得到的,你就会明白。这是删除额外的空间,我不想这样。谢谢。这个XML只是问我问题的一个示例。在我工作的地方,我无法访问任何其他软件包,也无法下载这些软件包。你可以使用PHP、python、Java、C等编程语言。。。可能是预先安装的。它们都有用于XSL转换的工具。如果你说出一种可用的语言,我可能更喜欢这种语言的例子。是的,我知道。但是您可以从shell脚本中调用类似于
python remove\u name\u attr.py的东西。py也不支持python:(.它必须是regex风格的。我的意思是我得到了我想要的。我非常接近。只是我不知道为什么
非常感谢。我明天会检查这个答案,如果正确就接受。不幸的是,我使用的服务器不支持
gsub
。你有什么
awk
版本?试试
awk--version
Appare我很抱歉,它确实支持gsub。当我问这个问题时,有一件事我没有想到。我不知道字段号。例如,我不知道
Name
$2
。可能是
$3
。有没有办法让它检查所有内容?更改为
awk'/Name/{gsub(/Name=“[^]*“/,x)}1'文件
,它将删除行中任何带有名称的字段。
xsltproc remove.name.xsl config.xml
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>