That code doesn't do what you say it does. It searches through the file then prints the line after your target line, and every 3rd line after that till the end of the file. The reason is that the inner while loop continues until the file ends. Also the readline doesn't do what you mean for it to do, it actually reads a line from the file (<FILE>) then uses that as a argument to readline. If you had used warnings it would have complained about a readline on unopened filehandle. readline(FILE)is the same as <FILE> and is not restricted to a while loop so where ever you read that is wrong. Here is a way over commented piece of code that does what you say you want :)

use warnings; use strict; my $success = 0; open(FILE, "<", "dumptape.log") or die "Couldn't open dumptape.log: $!"; while (<FILE>) { if (/Dump phase number 3/) { # We found the line my $line = <FILE>; # read the next line print $line; # print the next line <FILE>; # Skip a line <FILE>; # Skip a line $line = <FILE>; # read the fourth line print $line; # print the fourth line $success = 1; # we found it last; # So we're done! } } close FILE; if (! $success) { print "Failed dump log\n"; }

In reply to Re: Extracting Text Lines by tedrek
in thread Extracting Text Lines by mikevanhoff

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.