-1

I am struggling with sed expression.

cat sample.xml:

<pro path="broad" name="cd/broad" remote="bit" rev="5e2b78f768dc8dd6a7d1a1153942684993b40430" groups="app" />

Note: rev could also be equal to "master" or "commitid"

Expression:

sed -i "s/^\([[:space:]]*\)\(<pro\)\([[:space:]]\)\(path=.*\)\(rev=\)\([0-9a-f]{40}\)\([[:space:]]\)\(.*\)/\1\2\3\4\5blah\7/g" sample.xml

I want to change revision=5e2b78f768dc8dd6a7d1a1153942684993b40430 to rev=blah

My output should be:

<pro path="broad" name="cd/broad" remote="bit" rev="blah" branch="master" groups="apps" />

Could someone please help me fix my sed expression?

kvantour
  • 25,269
  • 4
  • 47
  • 72
john
  • 1
  • 2
  • 2
    Your XML is invalid, the `rev` value should be double quoted – oliv Nov 13 '18 at 15:56
  • Thanks for the comment, I tried with putting the double quotes but still it doesn't work – john Nov 13 '18 at 15:59
  • 1
    You ask for a regex to parse your HTML. [Never parse HTML or XML with a regex](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) you might meet the pony. – kvantour Nov 13 '18 at 16:00

1 Answers1

0

The way forward is to make use of an XML parser such as xmlstarlet:

xmlstarlet ed --update '//pro/@rev' --value "blah" xmlfile.xml

Note, since we do not have a clear picture of your full XML, this might not work.

Remark: if you want to add the branch attribute you can do it in a single go:

xmlstarlet ed --update '//pro/@rev' --value "blah" \
              --insert '//pro' --type attr --name "branch" --value "master" xmlfile.xml
kvantour
  • 25,269
  • 4
  • 47
  • 72
  • Thank you for brief explanation and showing me a different approach to this problem :) – john Nov 13 '18 at 16:28