in reply to Help Matching Sections of a file

Gotta plug the .. operator (untested):
my $port = "1234"; open FILE, blah blah blah or die; while (<FILE>) { print if /Port\s+$port/ .. /^#---/; } close FILE;

Replies are listed 'Best First'.
Re: Re: Help Matching Sections of a file
by eXile (Priest) on Apr 05, 2004 at 02:57 UTC
      Gotta plug the .. operator (untested):
    Nice one TilRMan! I was playing with it some more to get rid of the '#----' and I came up with:
    #!/usr/bin/perl my $port = $ARGV[0] or die "usage: $0 <portnumber>\n"; while (<DATA>) { print if /Port\s+$port/ .. /^#---/ and !/^#---/; } __DATA__ Port 119 NNTP * Microsoft Ports-Microsoft Exchange supports a News server running at + this port. * RFC977 * RFC1036 #------------------------------------------- Port 120 CFDP (UDP) Coherent File Distribution Protocol (CFDP) * RFC1235 #------------------------------------------- Port 123 NTP (UDP) NTP (Network Time Protocol) * RFC2030 * RFC1129 #--------------------------------------------
    Is there a way to do this with the '..' or '...' operator without having to do the extra match (the and !/^#---/ part)?
      How about (not well tested):
      while (<FOO>) { print if /Port\s+$port\b/ .. /^#---/ && last; }
      The last also fixes the (presumed) problem of looping through the whole file even after you've found the relevant section. Notice the \b too which I forgot.
        What if you have multiple iterations of the same target search and you want to print them all to stdout? Your solution would fail in this arena.