Search code examples
bashxmlstarlet

change content between tags HTML


I have an xml file as follows

<firewall>

<custom_list max_entry_num="50">
<id>1</id>
<desc>first</desc>
<rule>!!!!!</rule>
</custom_list>

<custom_list max_entry_num="50">

<id>2</id>
<desc>seconde</desc>
<rule> !!!!!! </rule>

</custom_list>
</firewall>

I want to change the content between the tags, for this I use the following command:

xmlstartlet ed -u "/firewall/custom_list/rule' -v "My First Text" old.xml >new.xml

but this text is in the two tags, When id =1 and id = 2.

<custom_list max_entry_num="50">
<id>1</id>
<desc>first</desc>
<rule>MY FIRST TEXT</rule>
</custom_list>
<custom_list max_entry_num="50">
<id>2</id>
<desc>second</desc>
<rule>MY FIRST TEXT</rule>

I want to:

  • When id equals 1 I put "MY FIRST TEXT"
  • when id equals 2 I put "MY SECOND TEXT"

How can I do this in bash?


Solution

  • xmlstarlet ed -u "/firewall/custom_list/rule[../id = '1']" -v "My First Text" -u "/firewall/custom_list/rule[../id = '2']" -v "My second Text" old.xml > new.xml