I have got an xml file containing some attributes like
<string name="my/ attribute" optional="true">
<description>some text</description>
<value>some text again</value>
</string>
I would like to change the value (which does not necessarily have to be “some text again”) by the string “none”. I tried the following on the command line:
awk '/<string name="my\/ attribute" optional="true">/,/<\/string>/ {sub(/<value>(.*)<\/value>/,"<value>none</value>")}1' my.xml > my_new.xml
This somehow works ok, but the result is as follows:
<string name="my/ attribute" optional="true">
<description>some text</description>
<value>some text again<\/value>
</string>
Why is the / (slash) in the tag escaped?
Thanks a lot for your help,
Daniela.
Assuming the inconsistencies in your question that Richard pointed out are accidental:
This is WEE bit safer than your script, in that it will handle minified XML (i.e. whitespace removed, all on e line), but it won’t handle
<value>that is split over multiple lines.I do recommend looking in to Perl’s XML::Simple or PHP’s SimpleXML. It won’t be a one-liner, but it will work MUCH more reliably.