In addition to what the other replies above have said (which is the way I would do it), I would strongly recommend using @ARGV to get your input parameters (input file name and pattern to look for). Something like this:
#!/usr/local/bin/perl use strict; use warnings; my $Usage = "Usage: $0 search-pattern data.file\n"; ( @ARGV == 2 and -f $ARGV[1] ) or die $Usage; my ( $pattern, $ifile ) = @ARGV; open( IN, "<", $ifile ) or die "$ifile: $!\n$Usage"; $/ = "\n1"; # use FORTRAN page break as input record separator my $opattern = $pattern; $opattern =~ s/[^\w.-]/_/g; # limit range of characters to be my $ofile = "$ifile.$opattern"; # used in output file name open( OUT, ">", $ofile ) or die "$ofile: $!\n"; while (<IN>) { print OUT if ( /\n0 EID:\s+$pattern\s+/ ); } close OUT;
When you do it that way, it's easier to automate multiple runs (extract a given pattern from several files, or several patterns from one file, or several patterns from several files, etc...), as well as being easier, quicker and less error-prone to run the script manually.

In reply to Re: REGEX on multiple lines by graff
in thread REGEX on multiple lines by igotlongestname

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.