6

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.

msanford
  • 11,803
  • 11
  • 66
  • 93
Zeeshan
  • 858
  • 4
  • 16
  • 30

1 Answers1

9

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
konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • i want to change the value of abc.xml element i.e "filename" to the value 'abc'. I think my question is not well stated sorry – Zeeshan Aug 13 '13 at 15:06
  • if i use #!/bin/sh value='abc' sed -i "s|filename|$value|g" testxml.xml then the filename changes to abc not the value of the filename i.e img_000001016592.png. I want to change img_000001016592.png to abc – Zeeshan Aug 13 '13 at 15:09
  • 1
    @Zeeshan I made an update please check. This would only work with single lines though. If there values are multi-lined between I suggest that you use a XML module for it instead in other languages like Perl or Ruby. – konsolebox Aug 13 '13 at 15:14