我有一个类似于以下格式的XML文件:
<name>property1</name> <fullName>property1</fullName> <info> #property info# </info> <value> <current>true</current> <default>false</default> </value> <name>property2</name> <fullName>property2</fullName> <info> #property info# </info> <value> <current>true</current> <default>false</default> </value> <name>property3</name> <fullName>property3</fullName> <info> #property info# </info> <value> <current>true</current> <default>false</default> </value>xml文件包含数百个此类属性。 我想只将一些属性的当前标记的值(比如property2)从true更新为false。 如何使用Unix命令执行此操作?
我是Unix新手,我正在为此编写一个Unix shell脚本。 任何帮助将不胜感激。
谢谢!
I have an XML file similar to the below format:
<name>property1</name> <fullName>property1</fullName> <info> #property info# </info> <value> <current>true</current> <default>false</default> </value> <name>property2</name> <fullName>property2</fullName> <info> #property info# </info> <value> <current>true</current> <default>false</default> </value> <name>property3</name> <fullName>property3</fullName> <info> #property info# </info> <value> <current>true</current> <default>false</default> </value>The xml file contains hundreds of such properties. I want to update the value of current tag of a few properties only (say property2) from true to false. How can I do this using Unix commands?
I'm new to Unix and I'm looking to write a Unix shell script for this. Any help will be greatly appreciated.
Thanks!
最满意答案
我想,这对你有用:
http://www.unixcl.com/2010/01/replace-values-in-xml-using-sed-and-awk.html
后来编辑:
这样做:
cat asd.xml | awk '{ x[NR] = $0 } END { for ( i=1 ; i<=NR ; i++ ) { if (x[i] ~ /<name>property2/ ) {x[i+4]=" <current>false</current>"}print x[i] }} ' > newfile.xml其中asd.xml是原始文件,newfile.xml是新文件。 请务必先备份原始文件!
I think, that this will be useful for you:
http://www.unixcl.com/2010/01/replace-values-in-xml-using-sed-and-awk.html
Later Edit:
This will do it:
cat asd.xml | awk '{ x[NR] = $0 } END { for ( i=1 ; i<=NR ; i++ ) { if (x[i] ~ /<name>property2/ ) {x[i+4]=" <current>false</current>"}print x[i] }} ' > newfile.xmlwhere asd.xml is the original file, and newfile.xml the new one. Be sure to back up the original file first!
更多推荐
发布评论