Question

I have an XML file similar to the below format:

<name>property1</name>
    <fullName>property1</fullName>
    <info> #property info# </info>
    <value>
      <current>true</current>
      <default>false</default>
    </value>

<name>property2</name>
    <fullName>property2</fullName>
    <info> #property info# </info>
    <value>
      <current>true</current>
      <default>false</default>
    </value>

<name>property3</name>
    <fullName>property3</fullName>
    <info> #property info# </info>
    <value>
      <current>true</current>
      <default>false</default>
    </value>

The xml file contains hundreds of such properties. I want to update the value of current tag of a few properties only (say property2) from true to false. How can I do this using Unix commands?

I'm new to Unix and I'm looking to write a Unix shell script for this. Any help will be greatly appreciated.

Thanks!

Was it helpful?

Solution

I think, that this will be useful for you:

http://www.unixcl.com/2010/01/replace-values-in-xml-using-sed-and-awk.html

Later Edit:

This will do it:

cat asd.xml | awk '{ x[NR] = $0 } END { for ( i=1 ; i<=NR ; i++ ) { if (x[i] ~ /<name>property2/ ) {x[i+4]="      <current>false</current>"}print x[i] }} ' > newfile.xml

where asd.xml is the original file, and newfile.xml the new one. Be sure to back up the original file first!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top