I have a text file that looks something like this:

A21 103 A22 103 A23 103 A25 103 A26 103 A27 103 12.3 7.3 .0289 179 2 5 1 4 3 1 + 0 0 0 A28 103 4.5 4.2 .0000 479 1 0 0 0 0 0 + 0 0 0 10.2 9.6 .0188 250 2 5 0 4 2 3 + 0 0 0 10.2 9.6 .0188 250 2 5 0 4 2 3 + 0 0 0 A29 103 4.5 4.2 .0000 479 1 0 0 0 0 0 + 0 0 0 10.2 9.6 .0188 250 2 5 0 4 2 3 + 0 0 0 A30 103 A32 103 4.5 4.2 .0000 479 1 0 0 0 0 0 + 0 0 0 10.2 9.6 .0188 250 2 5 0 4 2 3 + 0 0 0 A33 103 4.5 4.2 .0000 479 1 0 0 0 0 0 + 0 0 0 10.2 9.6 .0188 250 2 5 0 4 2 3 + 0 0 0 A34 103 A36 103 4.5 4.2 .0000 479 1 0 0 0 0 0 + 0 0 0

The data is separated by tabs or spaces

I wish to be able to read in the file, remove the lines with two batches of text(eg.'A21 103') unless they are followed by a longer line of data(eg.'12.3 7.3 .0289'...).

If they are followed by a longer line of data, to read the two batches of text and insert them at the beginning of the subsequent longer lines until the next short batch is found. And so on through the file.

In this example, I want the output therefore to look like this:

A27 103 12.3 7.3 .0289 179 2 5 1 4 3 + 1 0 0 0 A28 103 4.5 4.2 .0000 479 1 0 0 0 0 + 0 0 0 0 A28 103 10.2 9.6 .0188 250 2 5 0 4 2 + 3 0 0 0 A28 103 10.2 9.6 .0188 250 2 5 0 4 2 + 3 0 0 0 A29 103 4.5 4.2 .0000 479 1 0 0 0 0 + 0 0 0 0 A29 103 10.2 9.6 .0188 250 2 5 0 4 2 + 3 0 0 0 A32 103 4.5 4.2 .0000 479 1 0 0 0 0 + 0 0 0 0 A32 103 10.2 9.6 .0188 250 2 5 0 4 2 + 3 0 0 0 A33 103 4.5 4.2 .0000 479 1 0 0 0 0 + 0 0 0 0 A33 103 10.2 9.6 .0188 250 2 5 0 4 2 + 3 0 0 0 A36 103 4.5 4.2 .0000 479 1 0 0 0 0 + 0 0 0 0

The data lines may be in batches of one, two, three, four or even five lines before the short line which is actually a kind of a heading for the data that follows below it.

(The 'headings' without 'data' below them are not required and need to be dropped from the output, as stated above.)

I cannot find a way to do this.

I have googled a lot for examples but without any success. Hope someone can teach me how to do this.

Thank you very much for your help.


In reply to Insert Text which is Read from the Line Above by jlb333333

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.