Using sed/awk, I need to remove all lines in a file from the first occurrence of pattern1 up-to (but not including) the last occurrence of pattern2.
Consider the following text:
<entity name="good">
</entity>
<entity name="bad">
stuff to delete
</entity>
<entity name="bad">
stuff to remove
</entity>
<entity name="bad2">
</entity>
<entity name="deleteMe2">
</entity>
<entity name="bad2">
</entity>
<entity name="good">
</entity>
I would like the following outcome
<entity name="good">
</entity>
<entity name="bad2">
</entity>
<entity name="good">
</entity>
I know how to do a range in sed, but can't figure out how to match the last occurrence of 'bad2' and not include it in the delete. The below of course will not work as it will match the first bad2 and not remove the 'deleteme2' or 2nd occurrenc of 'bad2'.
sed -i '/<entity name="bad"/,/<entity name="bad2"/d' file.xml
There can be hundreds of 'bad'/'deleteMe2'/'bad2' lines in the file I am dealing with, so a simple line count won't work. I am fine if this is multiple commands (it does not have to be just a single one), but the more efficient the better because the file being modified can be quite large. As well, the -i is because I want to do an in place delete of the lines between.
NOTE: I am more familiar with SED than I am with AWK, but I am open to all the help I can get:)