0

I have a question with I can't figure out.

I have in xml file something like this:

<tag desc="some desc can be different" dep="dep" >value</tag>

I wanna change this like using sed to:

<tag desc="NEW DESC" dep="dep" >value</tag>

My question is: can I use sed to replace text between "<tag ... >" with new one?

Thank you for help :)

elpasso
  • 99
  • 6
  • 3
    XML and HTML are *not* regular languages. As such, trying to work some magic with RE tools like `sed` to parse HTML may be a recipe for disaster. – DopeGhoti Jan 13 '14 at 08:16
  • 2
    You sure can, but keep in mind that [regexes are not for parsing (X)HTML](http://stackoverflow.com/a/1732454/1807643). – KeyNone Jan 13 '14 at 08:16

2 Answers2

4

Since you are not trying to really parse the xml sed can help you:

sed -i 's/\(<tag[^>]*[ ]*desc[ ]*\)=[ ]*"[^"]*"/\1="NEW DESC"/g' input.xml

But if you want to have a robust solution, use xmlstarlet:

xmlstarlet edit -L -u "//tag/@desc" -v "NEW DESC" input.xml
perreal
  • 94,503
  • 21
  • 155
  • 181
0

Thanks for quick answer :)

I have done something like this:

typeset repTagdesc='desc="NEW DESC" dep="dep"'
sed -i "s&\(<tag\).*\(>.[0-9]\)&\1 ${repTagdesc}\2&" input.xml

Is this solution OK? I am not familiar with sed, thats why I am asking. I check it and its works for:

<tag>10</tag>
<tag desc="some desc" dep="dep">20</tag>
<tag desc="other desc" dep="dep">30</tag>
elpasso
  • 99
  • 6