in reply to
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”):
A record which marks the start of some area-of-interest. Such as, in this case, TABLE.
A record which marks the end of an area (and the generation of output based on accumulated data), such as END.
A record which marks data to be gathered in-memory in anticipation of future output, such as DATAnn.
A record whose presence is expected but otherwise uninteresting, such as HEADn.
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.