2

I have a XML file, MyFile.xml on my Linux machine.

<?xml version="1.0" encoding="UTF-8"?>
  <project>
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
          <modelVersion>4.0.0</modelVersion>
          <groupId>com.mycompany</groupId>
              <env name="envvar" value="/opt/environment/environment.properties" />
              <env name="JDBC_Driver"  value="/opt/JDBC/ojdb6.jar" />
              <env name="agent/enable" value="true" />
</project>

I need to change the value to "false" (when it is true) and "true" (when it is false) for the env name="agent/enable" tag in the XML file whenever I get a requirement.

I just wanted to use some shell script for this. I know this can be done using Sed command in Linux, but I'm not good at regular expressions. So that whenever I get a requirement to change I can just run a script.

Note: script should change the XML file and should be saved on the file system with same name.

halfer
  • 19,824
  • 17
  • 99
  • 186
Cherry
  • 35
  • 1
  • 5
  • 1
    In general, we encourage people to give problems such as this a go, and we try to discourage "do it for me" questions - primarily because it may not help the OP learn anything. If you can edit even the start of this into your question, it really helps both you and us. – halfer May 03 '15 at 10:34
  • Apologies for that halfer!!! Cheers :) – Cherry Jun 08 '15 at 11:18
  • (Sorry, I have rolled back your edit. We tend to edit questions to be as succinct as possible here, remove excessive formatting, use inline code formatting rather than bold, and trim requests for urgent help). – halfer Jun 08 '15 at 11:23
  • Possible duplicate of [Replace dynamic content in XML file](http://stackoverflow.com/questions/13369933/replace-dynamic-content-in-xml-file) – tripleee Sep 11 '16 at 11:14

2 Answers2

5

Do not attempt to edit XML with plain-text tools; it can only end badly. Nobody expects their XML tools to break when they insert a newline into the XML or change a <foo/> node to <foo></foo>, but they will if they're based on sed (and not ridiculously complex).

Instead, use a proper XML-parsing tool such as xmlstarlet:

xmlstarlet ed --inplace -u '/project/env[@name="agent/enable"]/@value' -x 'string(not(. = "true"))' filename.xml

Explanation

xmlstarlet ed --inplace -u <foo> -x <bar> filename.xml

updates nodes that fit the XPath expression <foo> to have the value of the XPath expression <bar>,

/project/env[@name="agent/enable"]/@value

is an XPath expression that selects the value attribute of env nodes whose name attribute is agent/enable and that are a child of the project root node -- this should describe the value you want to change adequately -- and

string(not(. = "true"))

is an XPath expression that is false if the currently inspected value is "true" and true otherwise. The currently inspected value is the old value of the value attribute that's being updated.

Wintermute
  • 42,983
  • 5
  • 77
  • 80
2

Here you are:

sed -i '/<env\s\+name="agent\/enable"/s/value="true"/value="false"/' f.xml
tivn
  • 1,883
  • 14
  • 14