用于更改xml元素的shell脚本[复制]

参见英文答案 > Replace dynamic content in XML file                                    3个
我想将我的xml“abc.xml”元素的值更改为存储在变量$value中的值,即

$value =’abc’;

<annotation>
    <filename>img_000001016592.png</filename>
    <folder>Rec_20121219_171905</folder>
    <source>
        <sourceImage>The MIT-CSAIL database of objects and scenes</sourceImage>
        <sourceAnnotation>LabelMe Webtool</sourceAnnotation>
    </source>
    <imagesize>
        <nrows>481</nrows>
        <ncols>640</ncols>
    </imagesize>
</annotation>

需要shell脚本,它有一个变量,它包含变量中的值,然后将abc.xml的元素文件名的值更改为变量中的值.

解决方法

也许你的意思是使用sed.

value='abc'
sed -i "s|abc.txt|$value|g" abc.xml

您必须在shell中运行它,或者作为带有标题#!/ bin / sh的shell脚本运行.

—-更新—-

#!/bin/sh
value="something"
sed -i "s|\(<filename>\)[^<>]*\(</filename>\)|\1${value}\2|" abc.xml

如果需要在一行中替换多个实例,请将g添加到sed命令.

sed -i "s|\(<filename>\)[^<>]*\(</filename>\)|\1${value}\2|g" abc.xml

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念