用于更改xml元素的shell脚本

Zee*_*han 5 xml bash shell

我想将我的xml"abc.xml"元素的值更改为存储在变量$ value ie中的值

$ 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>
Run Code Online (Sandbox Code Playgroud)

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

kon*_*box 8

也许你的意思是使用sed.

value='abc'
sed -i "s|abc.txt|$value|g" abc.xml
Run Code Online (Sandbox Code Playgroud)

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

----更新----

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

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

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