Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shell script for the changing the element of xml [duplicate]

Tags:

bash

shell

xml

I want to change the value of my xml "abc.xml" element to the value that is stored in the variable $value i.e

$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>

The shell script is required which has one variable and it contains the value in the variable and then change the value of the element filename of the abc.xml to the value in variable.

like image 868
Zeeshan Avatar asked Aug 13 '13 14:08

Zeeshan


1 Answers

Perhaps you mean to use sed.

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

You would have to run that in a shell, or as a shell script with the header #!/bin/sh.

---- Update ----

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

Add g to the sed command if you need to replace multiple instances in one line.

sed -i "s|\(<filename>\)[^<>]*\(</filename>\)|\1${value}\2|g" abc.xml
like image 191
konsolebox Avatar answered Sep 20 '22 17:09

konsolebox