I have a file test.xml with the following content :
<body>
<content>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam.</p><p>Maecenas ligula massa, varius a, semper congue, euismod non, mi. Proin porttitor, orci nec nonummy molestie, enim est eleifend mi,..</p><p>
<MEDIAREF localid="HTM37c2ae34-b92c-11e1-86ab-e6b6e8e434a7-0"/>
</p><p>Duis arcu massa, scelerisque vitae, consequat in, pretium a, enim. Pellentesque congue. Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum augue</p><p>
<MEDIAREF localid="HTM37c2ae34-b92c-11e1-86ab-e6b6e8e434a7-1"/>
</p><p>Praesent egestas leo in pede. Praesent blandit odio eu enim.
</p>
</content>
</body>
...
<ZONEMEDIAS>
<MEDIA localid="HTM37c2ae34-b92c-11e1-86ab-e6b6e8e434a7-0">
<MEDIAPROPRIETES>
<PROPRIETE value="HTM" name="type"/>
</MEDIAPROPRIETES>
<CODEMEDIA><object width="493" height="370"><param name="movie" value="http://www.youtube.com/v/Rxxxxxfr_FR&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/RxxxxxfrR&amp;rel=0" type="application/x-shockwave-flash" width="493" height="370" allowscriptaccess="always" allowfullscreen="true"></embed></object>
</CODEMEDIA>
</MEDIA><MEDIA localid="HTM37c2ae34-b92c-11e1-86ab-e6b6e8e434a7-1">
<MEDIAPROPRIETES>
<PROPRIETE value="HTM" name="type"/>
</MEDIAPROPRIETES>
<CODEMEDIA><blockquote class="twitter-tweet" lang="fr"><p>second texte to replace <a href="https://twitter.com/xxxx" data-datetime="2012-06-15T01:12:03+00:00">Juin 15, 2012</a></blockquote>
<script src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
</CODEMEDIA>
</MEDIA>
</ZONEMEDIAS>
I expected replace
<MEDIAREF localid="HTM37c2ae34-b92c-11e1-86ab-e6b6e8e434a7-0"/>
by
<object width="493" height="370"><param name="movie" value="http://www.youtube.com/v/Rxxxxxfr_FR&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/RxxxxxfrR&amp;rel=0" type="application/x-shockwave-flash" width="493" height="370" allowscriptaccess="always" allowfullscreen="true"></embed></object>
and do the same for ::
Is there any way to achieve this in bash please ?
I tried something like this to retrieve value if localid
grep "<MEDIAREF localid=.*\".>" test.xml | sed -e "s/^.*<MEDIAREF localid=/<MEDIAREF localid=/" | cut -f2 -d"\"" | cut -f1 -d"\""
but I don’t figure out how to replace after
Someone can help me please?
In bash
EDIT, After your comment I understand better what you want to do, bash is not the best solution and may not be safe, perl is better, but it works with your example. Here a solution with bash :
Other solution in perl: