indieBoy has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, This is my first posting! I have a text file similar to:
Summary 1000(1) Date .... Column1 Col2 Col3 ..... .... .... Summary xxx(x)
I want to extract those lines between the first summary and the second (occurs multiple number of times). I know our to extract lines with different patterns but not sure how to do this where the pattern is the same. The only difference is the xxx. Thanks

Replies are listed 'Best First'.
Re: extract lines between same pattern
by targetsmart (Curate) on May 27, 2009 at 12:24 UTC
    what have you tried in first place, any code?.

    fine having said that
    the pattern can be

    m/^Summary\s*\d+\(\d+\)/
    see perlretut

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
      use FileHandle; $fh = new FileHandle '<input.txt'; $in_body = 0; while (<$fh>) { if ($in_body) { if (m{SUMMARY.*?>}i) { $in_body = 0; } else { ($row1, $row2) = split ; # print "$row1", "\t", "$row2" , "\n"; print; } } elsif (m{/SUMMARY.*?>}i) { $in_body = 1; # print; }
      Here i can get the data where the end is /SUMMARY. For the column data I want to convert to row data. So that i would have:
      summary col1 col2 col3 .... SUMMARY XXXX SUMMARY YYY
        If you use parens, like m/SUMMARY\s(.*)\n/i then $1 will contain whatever matched .*

        -- Time flies when you don't know what you're doing
Re: extract lines between same pattern
by shmem (Chancellor) on May 27, 2009 at 18:02 UTC