微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用于更改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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。