The short answer is that you need to use $nextLine = <INPUT> to get the next line after you match, then use last to leave the loop.

Here's something I patched together:

#!/usr/bin/perl -w # Next After Match .. PM node 418383 my $filename = shift; open ( INPUT, $filename ) or die "Unable to open $filename: $!"; my $string = shift; my $nextLine; while(<INPUT>) { if ( /$string/ ) { $nextLine = <INPUT>; last; } } close ( INPUT ); if ( defined ( $nextLine ) ) { print "INFO: $string found in file $filename, next line is:\n"; print $nextLine; } else { print "INFO: $string not found in file $filename.\n"; }
When I run it on itself I get the following:
[foo@bar dev]$ perl -w nam.pl nam.pl print INFO: print found in file nam.pl, next line is: print $nextLine; [foo@bar dev]$ perl -w nam.pl nam.pl whoosat INFO: whoosat not found in file nam.pl.
Seems to work.

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds


In reply to Re: Getting next line after matched one by talexb
in thread Getting next line after matched one by ramthen

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.