#! perl -slw use strict; $|++; # Disable buffering on STDOUT for demo my $re_multi = qr[ # Match 3 lines ^name\s*\n # First starts with "name" (and maybe some whitespace? ^-+\n # Second consists entirely of '-'s (^.+)\n # Third has the stuff we want so capture to $1 ]mx; # Allow match across line boundries. # Ignore incidental white space my $buffer = ''; # Init our buffer to null # Grab a managable chunk of data. # 16 is silly for demo only. # My test show that 32/64 k seems about right on my system. # The length() call ensures residual is retained while (sysread( DATA, $buffer, 16, length $buffer)) { # find all occurances in buffer. while($buffer =~ m[$re_multi]g ) { print $1; # Do something with them # stop the buffer grwing bigger than necessary # by discarding everything upto the end of the last match $buffer = substr($buffer, pos($buffer) ); } # This line defends against the buffer growing very large if two occurances # of the pattern are a very long way apart in the file. # It works (in the case) by discarding stuff that that cannot be part # of what we are looking for (and could probably be improved upon). # This must be tailored on a case-by-case basis. $buffer = substr( $buffer, pos($buffer) -1 ) if $buffer =~ m[\n(?!name)]gc; } __DATA__ Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap name ---------------------------------------- 1 23 4.5 678e9 Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap name ---------------------------------------- 1 23 4.5 678e9 Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap name ---------------------------------------- 1 23 4.5 678e9 Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap Loads'a junk a garbage and irrelevent crap