I am deleting sections of tags from an XML and need to find the end tag to stop at.
Example XML
<SearchPattern>
<something></something>
</SearchPattern>
<OtherTag></OtherTag>
<SearchPatternHit2>
<something></something>
</SearchPatternHit2>
Example XML Desired Output after iteration 1
<OtherTag></OtherTag>
<SearchPatternHit2>
<something></something>
</SearchPatternHit2>
Example XML Desired Output after iteration 2
<OtherTag></OtherTag>
My current attempt uses sed to find the first occurrence line number like this:
start_line = $(grep -n "<${SEARCH_PATTERN}" ${FILE_PATH} | head -1| cut -f1 -d':')
The output for this is only the line number integer.
I then try to get the line of the closing tag using:
finish_line = $(sed -n "${start_line},$ !d;/<\/${LEADING_TAG}>/=" ${FILE_PATH} | head -1)
I believe the result of finish line is incorrect when multiple instances of the search pattern exist, so it does not get the first closing tag.
In the example, the pattern is found on line 1, and 7. on the first pass, start_line=1, but finish_line is not returning 3 as it should.
After getting these two values, I call a simple sed statement that is working.
sed -i "${start_line}, ${finish_line}d" ${FILE_PATH}
What would be a better way to obtain the line of the closing tag from each block?