wildcard substitution, remove html tag including content, sed

this is test.html:
<html><body>
<div>
<div class="start">delete this</div>
</div>
<div><div class="start">delete this</div>leave this</div>
</body></html>
We want to delete all html tags div.start and its content:
We choose a start pattern <div class="start">:
Choose end patern </div>. But we do not want to choose the wrong end pattern:
Make that start and end pattern is always on single line.

To do it so add "brake rule" (\n) on the left and right side of each start pattern

Translating to sed language:
Replace <div class=\d034start\d034> with \n<div class=\d034start\d034>\n

Add the "brake rule" (\n) on the left and right side on end pattern.
Replace <\/div> with \n<\/div>\n

At the end it looks like:
sed "s/<div class=\d034start\d034>/\n<div class=\d034start\d034>\n/g;s/<\/div>/\n<\/div>\n/g" test.html
Now use sed again to remove lines between start and end pattern inclueding pattern:sed "/<div class=\d034start\d034>/,/<\/div>/d":
And finally remove all brake rules with:sed ":a;N;$!ba;s/\n//g":
Here is whole string:
sed "s/<div class=\d034start\d034>/\n<div class=\d034start\d034>\n/g;s/<\/div>/\n<\/div>\n/g" test.html | sed "/<div class=\d034start\d034>/,/<\/div>/d" | sed ":a;N;$!ba;s/\n//g"

No comments: