This removes the need for magic numbers in the code, just change the two strings in the BEGIN block.
It also copes the close tag not being the first item on that line.
BEGIN {
inTag = 0
tagToRemove = "<tag id=\"name\">"
tagEndMarker = "</tag>"
}
{
if (inTag == 0) {
position = match ($0,tagToRemove)
if (position == 0)
print $0
else {
print substr($0,1,(position-1))
inTag = 1
}
} else {
position = match ($0,tagEndMarker)
if ( position != 0 ) {
inTag = 0
print substr($0,position + length(tagEndMarker))
}
}
}
-------------
My rest data is:
<tag1>
keep line 1
</tag1><tag id="name">
<removed line>
another removed line
</removed line>
</tag><tag2>
more lines
</tag2><tag id="asdf">
more stuff
</tag>
last line
and the output I get is:
<tag1>
keep line 1
</tag1>
<tag2>
more lines
</tag2><tag id="asdf">
more stuff
</tag>
last line
|