Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace XML values in bash in linux

Tags:

linux

bash

sed

I have the following XML data:

<info>
    <data>
        <name>my_image</name>
        <value>site.com/image1.img</value>
    </data>
    <data>
        <name>my_link</name>
        <value>site.com/p_info</value>
    </data>
</info>

I want to replace all the site.com to siteimag.com of every my_image value (just for the my_image attributes).

So the result will be:

<info>
    <data>
        <name>my_image</name>
        <value>siteimag.com/image1.img</value>
    </data>
    <data>
        <name>my_link</name>
        <value>site.com/p_info</value>
    </data>
</info>

How can it be done with sed command?

Thanks.

like image 459
iseif Avatar asked Sep 20 '25 20:09

iseif


1 Answers

sed '/my_image/{n;s/site.com/siteimag.com/}' file

Brief explanation,

  • /my_image/: search the line contained "my_image"
  • Once the string searched,
    • read the next line by using the command n.
    • s/site.com/siteimag.com/: do the substitution
like image 67
CWLiu Avatar answered Sep 22 '25 11:09

CWLiu