0

This is my XML.

<configGroup>
<type>SERVER_CONFIG</type>
  <configProperty id="Server48">
    <configProperty>
        <name>Server Name</name>
        <value>oaea_server1</value>
        <itemMetadata>
            <dataType>STRING</dataType>
            <scope>READ_ONLY</scope>
        </itemMetadata>
    </configProperty>
    <configProperty>
        <name>Listen Address</name>
        <value>server.com</value>
        <itemMetadata>
            <dataType>STRING</dataType>
            <scope>READ_WRITE</scope>
        </itemMetadata>
    </configProperty>
    <configProperty>
        <name>Listen Port</name>
        <value>6851</value>
        <itemMetadata>
            <dataType>INTEGER</dataType>
            <scope>READ_WRITE</scope>
        </itemMetadata>
    </configProperty>
</configProperty>
<configProperty id="Server6">
    <configProperty>
        <name>Server Name</name>
        <value>oacore_server2</value>
        <itemMetadata>
            <dataType>STRING</dataType>
            <scope>READ_ONLY</scope>
        </itemMetadata>
    </configProperty>
    <configProperty>
        <name>Listen Address</name>
        <value>server.com</value>
        <itemMetadata>
            <dataType>STRING</dataType>
            <scope>READ_WRITE</scope>
        </itemMetadata>
    </configProperty>
    <configProperty>
        <name>Listen Port</name>
        <value>7251</value>
        <itemMetadata>
            <dataType>INTEGER</dataType>
            <scope>READ_WRITE</scope>
        </itemMetadata>
    </configProperty>
</configProperty>

</configGroup>

XML contains lot of other type of server and my requirement is to delete all the servers which matches oaea_server.

Output should grep for below and delete the same from new XML

<configProperty id="Server48">
    <configProperty>
        <name>Server Name</name>
        <value>oaea_server1</value>
        <itemMetadata>
            <dataType>STRING</dataType>
            <scope>READ_ONLY</scope>
        </itemMetadata>
    </configProperty>
    <configProperty>
        <name>Listen Address</name>
        <value>server.com</value>
        <itemMetadata>
            <dataType>STRING</dataType>
            <scope>READ_WRITE</scope>
        </itemMetadata>
    </configProperty>
    <configProperty>
        <name>Listen Port</name>
        <value>6851</value>
        <itemMetadata>
            <dataType>INTEGER</dataType>
            <scope>READ_WRITE</scope>
        </itemMetadata>
    </configProperty>
</configProperty>

I have tried using grep and it works also but then I have hardcoded the lines before and after

grep -B 3 -n -A 22 oaea_server

But I know this is not the correct way as there could be changes in the xml any time and the automated script will fail.

Thanks

1

Using xmlstarlet this should do the trick with properly formed xml

xmlstarlet ed -d "//configProperty[.//value[contains(string(), 'oaea_server')]]" file1

Output has Server48 removed

<?xml version="1.0"?>
<configGroup>
  <type>SERVER_CONFIG</type>
  <configProperty id="Server6">
    <configProperty>
      <name>Server Name</name>
      <value>oacore_server2</value>
      <itemMetadata>
        <dataType>STRING</dataType>
        <scope>READ_ONLY</scope>
      </itemMetadata>
    </configProperty>
    <configProperty>
      <name>Listen Address</name>
      <value>server.com</value>
      <itemMetadata>
        <dataType>STRING</dataType>
        <scope>READ_WRITE</scope>
      </itemMetadata>
    </configProperty>
    <configProperty>
      <name>Listen Port</name>
      <value>7251</value>
      <itemMetadata>
        <dataType>INTEGER</dataType>
        <scope>READ_WRITE</scope>
      </itemMetadata>
    </configProperty>
  </configProperty>
</configGroup>
1
  • Thanks..the same worked. – Mintoo Mar 30 '20 at 4:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.