"sed" regex help: Replacing characters

Posted by powerbar on Super User See other posts from Super User or by powerbar
Published on 2012-03-28T09:26:05Z Indexed on 2012/03/28 11:33 UTC
Read the original article Hit count: 399

Filed under:
|

I want to change characters in a XML file by using sed. The input looks like this:

<!-- Input -->
<root>
  <tree foo="abcd" bar="abccdcd" />
  <dontTouch foo="asd" bar="abc" />
</root>

Now I want to change all c to X in the bar tag of the tree element.

<!-- Output -->
<root>
  <tree foo="abcd" bar="abXXdXd" />
  <dontTouch foo="asd" bar="abc" />
</root>

How is the correct sed command? Please consider, there can be more than one occurence of c (next to each other or not) in one tag...

I tried this myself, but it won't change multiple c, and it does append a X :(

sed -i 's/\(<tree.*bar=\".*\)c\(.*\"\/>\)/\1X\2/g' Input.xml

© Super User or respective owner

Related posts about regex

Related posts about sed