If you've ever wanted to grep through files for a match, and print the lines following the matching line, and your system's grep doesn't do that, here's something that should do the job. Of course it uses perl's regex syntax for the matching pattern.
#!/usr/bin/perl # # Grep for something and display the line and # the N lines following the match (default 1) # Prefix each line with the filename # if there is more than 1 file argument # Exit status: # 0 - a match was found # 1 - No match was found # >1 - An error was encountered (even if a match was found) use Getopt::Std; $usage_str = <<EOT; Usage: ngrep [-h] [-i] [-n #] [-q] pattern file1 file2 ... -h - Suppress file names when multiple file arguments are supplied -i - Do case-insensitive search -n - Number of lines to display after match (default: 1) -q - Treat pattern as a literal string instead of a regex -r - Don't reset count if line following matched line matches EOT getopts('qrhin:') or die $usage_str; die $usage_str unless @ARGV; $pttrn = shift; $pttrn = quotemeta $pttrn if $opt_q; $pttrn = '(?i)' . $pttrn if $opt_i; $pttrn = eval { qr/$pttrn/ } or die "$@$usage_str"; $n = length($opt_n) ? int($opt_n)+1 : 2; $filename = (@ARGV > 1 and ! $opt_h) ? sub { "$ARGV: " } : sub { '' }; $SIG{__WARN__} = sub { warn $_[0]; $error = 2 }; $status = 1; $i=0; while (<>) { ($status, $i) = (0, $n) if ! ($opt_r and $i) and /$pttrn/; $i--, print &$filename, $_ if $i; $i = 0 if eof; } exit($error || $status);

In reply to Grep - print matched line and next N lines by runrig

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.