in reply to Combining multiple lines into one line

When I consider problems like the one you describe, I freely admit that I’m heavily influenced by a tool ... awk ... that is known to be one of the (so to speak...) “inspirations for” Perl.   This tool (IMHO) encourages you to consider a file “line-type by line-type.”   It is a very-useful, generalized approach to problems of this type that generally consists of three questions:

  1. What type of line (record...) am I right-now looking at?
  2. Now that I know this ... what should I do with this type of line?
  3. Now that I’ve done this ... “am I finished yet?”   (And if so, what should I do?)

Bear with me, now...   You might, for example, decide that the “types of lines” might be:

Please let me refer you to man awk from this point forward.   awk is, in fact, a generalized tool to address this very type of problem, and it is one of the acknowledged inspirations for Perl.   No matter what tool you use to solve your problem, the approach that is encouraged by awk is (I think...) very useful food for thought.