问题
I am trying to edit a xml file wi9th a shell script. I have found some help like this. But the Structure of my XML file is little different. This is a example of my XML file:
<Code>
<Type name="abc">this is ABC</Type>
<Type name="xyz">this is XYZ</Type>
</Code>
Here I want to get the this is ABC
and this is XYZ
text and then append a String with them like this:
<Code>
<Type name="abc">this is ABC EDITED</Type>
<Type name="xyz">this is XYZ EDITED</Type>
</Code>
I can edit the 'abc'
like this:
xmlstarlet ed --inplace -u "/Code/Type[@name='abc']/@name" -v abc_edited ./abc.xml
But How can I edit the 'this is ABC'
String where 'this is ABC'
can be anything unknown?
回答1:
This could be done using awk
Content = (cat abc.xml | awk -f '[{}]' '/Type name="abc"/'{print$3} | sed 's/this is ABC/this is ABC EDITED/i')
来源:https://stackoverflow.com/questions/41034740/xml-file-structure-while-editing-xml-file-with-shell-script