how to update the xml file using xmlstarlet
You did not show your input document, but I assume it is the following, taken from the xmlstarlet documentation:
<xml> <table> <rec id="1"> <numField>123</numField> <stringField>String Value</stringField> </rec> <rec id="2"> <numField>346</numField> <stringField>Text Value</stringField> </rec> <rec id="3"> <numField>-23</numField> <stringField>stringValue</stringField> </rec> </table></xml>
xmlstarlet modifies the file, but the result is sent to standard output, not saved in the original file. Use another option --inplace
to modify the file in place:
$ xml ed --inplace -u "/xml/table/rec[@id='3']/@id" -v 5 rec.xml
Then:
$ cat rec.xml<?xml version="1.0"?><xml> <table> <rec id="1"> <numField>123</numField> <stringField>String Value</stringField> </rec> <rec id="2"> <numField>346</numField> <stringField>Text Value</stringField> </rec> <rec id="5"> <numField>-23</numField> <stringField>stringValue</stringField> </rec> </table></xml>
By the way, this question seems to ask something very similar to this question.
EDIT: As suggested by @npostavs, this option is listed in the edit help:
$ xml edit --help...-L (or --inplace) - edit file inplace...