Replace before and after string in vim



I have a large XML file that has essential information commented out for whatever dumb reason the author decided to do it.


It's similar to the following:



<book id="cat2" type="t" group="1234"> <!-- Group Name -->
<book id='ABC123' type='s'/> <!-- NameOfBookHere -->
<book id='etc456' type='s'/> <!-- Harry Potter -->
<book id='XYZ234' type='s'/> <!-- Jurassic Park --> //Notice variable space before the comment tag.
</book>


I'd like to be able to do a replace based on the first replace. Here's what I tried.



  1. I need to get rid of the beginning comment tag. I tried: :%s/\/> \+<!-- / name="

  2. I try another similar one for tags that aren't self closing :%s/> \+<!-- / name="

  3. Then replace the remaining: %s/ -->/"\/>


The results are something like this:



<book id="cat2" type="t" group="1234" name="Group Name"/>
<book id='ABC123' type='s' name="NameOfBookHere"/>
<book id='etc456' type='s' name="Harry Potter"/>
<book id='XYZ234' type='s' name="Jurassic Park"/>
</book>


Unfortunately, this affects <book> tags with group in it which aren't self closing.


Which means I'm left with thousands of self-closing <book>tags that aren't supposed to be self closing xml tags. It's not feasible to remove them manually of course.


Is there a way I can do another replace based on the lines that were affected by the first replace? Or is there another solution to my problem?


No comments:

Post a Comment