简体   繁体   English

在Shell脚本中需要帮助以查找和替换xml文件中的值

[英]Need help in shell scripting to find and replace value in xml file

I have the following XML format 我有以下XML格式

<object class="Class A">
<directory>someString1</directory>
<attr>
     <name>length</name>
     <value>6</value>
 </attr>
 <attr>
     <name>parent</name>
     <value>1</value>
 </attr>
 <attr>
     <name>Status</name>
     <value>1</value>
 </attr>
 <attr>
     <name>className</name>
     <value>Class A</value>
 </attr>
 <attr>
     <name>Instance</name>
     <value>InstanceValue</value>
 </attr>
 </object>
 ...
 <object class="Class D">
 <directory>someString4</directory>
 <attr>
     <name>length</name>
     <value>8</value>
 </attr>
 <attr>
     <name>parent</name>
     <value>1</value>
 </attr>
 <attr>
     <name>Status</name>
     <value>1</value>
 </attr>
 <attr>
     <name>className</name>
     <value>Class D</value>
 </attr>
 <attr>
     <name>Instance</name>
     <value></value>
 </attr>
 </object>
 ....

I need to find particular class D object only and find whether Instance value is empty for that object, If empty fill up with some value provided as an argument. 我只需要查找特定的D类对象,并查找该对象的实例值是否为空,如果为空,则用提供的某些值作为参数填充。 Please note that there can be multiple objects in the XML file and XML tags name and value are quite a bit repeated. 请注意,XML文件中可以有多个对象,并且XML标记的名称和值重复了很多。 Further, I need to do it with shell scripting only on suse Linux. 此外,我只需要在suse Linux上使用shell脚本来实现。

I am new to shell scripting and SED. 我是Shell脚本和SED的新手。 I tried my level best to find existing questions and answers in stackoverflow, but coulnd't find a relevant one. 我竭尽全力在stackoverflow中找到现有的问题和答案,但找不到相关的问题。 Any help is highly appreciated. 非常感谢您的帮助。

If you can use xmlstarlet , you can do something like: 如果可以使用xmlstarlet ,则可以执行以下操作:

xml ed -L -u "//object[@class='Class D']/attr[name='Instance'][value='']/value" -v "new value" input.xml

Note: The -L edits the file in-place. 注意: -L编辑文件。 Remove it if this is unwanted. 如果不需要,请将其删除。

Alternatively, you could use xsltproc to process the XML with XSLT: 另外,您可以使用xsltproc使用XSLT处理XML:

xsltproc -o output.xml stylesheet.xsl input.xml

Where stylesheet.xsl is: 其中stylesheet.xsl是:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="object[@class='Class D']/attr[name='Instance' and value='']/value">
    <xsl:copy>
      <xsl:text>new value</xsl:text>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Can you check if it works as you need? 您可以检查它是否可以根据需要工作吗?

#!/bin/bash

VALUE="NewValue"
sed -i data.xml -re "
/Class D/,/<\/object>/ {
    /<name.*>Instance<\/name>/,/<\/value>/ {
        s/(<value.*>)(<\/value>)/\1${VALUE}\2/
    }
}
"

It should find your class, then find name "Instance", and then insert new value if there is no value, otherwise it should not do anything 它应该找到您的类,然后找到名称“ Instance”,然后在没有值的情况下插入新值,否则应该什么也不做

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM