6

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>

1 つの変数を持ち、変数の値を含むシェル スクリプトが必要です。次に、abc.xml の要素ファイル名の値を変数の値に変更します。

4

1 に答える 1

9

おそらく、sedを使用するつもりです。

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

それをシェルで実行するか、ヘッダー付きのシェルスクリプトとして実行する必要があります#!/bin/sh

- - アップデート - -

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

g複数のインスタンスを 1 行で置き換える必要がある場合は、sed コマンドに追加します。

sed -i "s|\(<filename>\)[^<>]*\(</filename>\)|\1${value}\2|g" abc.xml
于 2013-08-13T14:59:40.193 に答える