使用shell脚本更新XML文件中的选定标记值(Update selected tag values in XML file using shell script)

编程入门 行业动态 更新时间:2024-10-28 08:31:21
使用shell脚本更新XML文件中的选定标记值(Update selected tag values in XML file using shell script)

我有一个类似于以下格式的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.xml

where asd.xml is the original file, and newfile.xml the new one. Be sure to back up the original file first!

更多推荐

本文发布于:2023-08-06 21:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455449.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   标记   文件   XML   shell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!