XML file structure while editing xml file with shell script

这一生的挚爱 提交于 2019-12-25 13:15:13

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!