yguw
yguw

Reputation: 844

set the property value for a single attribute in xml using bash script

I am using this XML file: https://github.com/apache/ranger/blob/master/ugsync/src/test/resources/ranger-ugsync-site.xml

Now, I have to update the value of the following property to false.

<property>
  <name>ranger.usersync.group.usermapsyncenabled</name>
  <value>true</value>
</property>

To update the value of property in the XML to false, I am using the below command in the bash script to get the updates done.

tag1=<name>ranger.usersync.group.usermapsyncenabled</name>
file=xml_file.xml
temporary=temp.xml

grep -A1 $tag1 $file | grep -v $tag1 | sed -e "s/^.*<value>/<value/" | sed -e "s/<value>true<\/value>/<value>false<\/value>/g" $file > $temporary

Problem: The script is updating all the other attributes in the file with value true to false. I just need to update the value of this attribute and not others. Input/help is appreciated. Thanks!

Upvotes: 0

Views: 647

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295281

Using XMLStarlet:

xmlstarlet ed \
  -u "//property[name='ranger.usersync.group.usermapsyncenabled']/value" \
  -v false \
  <in.xml >out.xml

The XPath expression used to determine what to change ensures that we only modify a value that is under a property with the appropriate name.

Upvotes: 4

Related Questions