in reply to text processing
This sort of problem is, actually, extremely common. It is, in fact, the inspiration for the awk tool that was one of the original inspirations for Perl.
In general, problems like this one can solved “text line by text line,” and can be reduced, algorithmically speaking, to four cases, all of which can (somehow) be recognized by the contents of the line (and/or by “beginning of file” and/or “end of file”):
Your immediate requirement could actually be addressed entirely by awk, and nothing else, and perhaps for this very reason you might elect to do so. In any case, the man-page hyperlinked above should now be read carefully and thoroughly.