#!/usr/bin/perl my $binfile = 'binfile'; my $outfile = 'testfile'; my $re1 = qr{blorf}; # that is the regex which saves the token my $re2 = qr{foo bar}; # that is the regex for following lines open my $in, '<', $binfile or die "'$binfile': $!\n"; open my $out, '>', $outfile or die "'$outfile': $!\n"; my $buf; # empty my $found; # not yet while(! eof $in) { if ($found && /$re2/g) { # do substitution s/$re2/quux $1 $found/; } elsif (/($re1)/g) { # if we match the start of a section, # we remember what we need and write all things read # up to the position of the current match. $found = $1; my $chunk = substr $_,0,length($1)+pos; print $out $chunk; substr $_, 0, length($1)+pos,''; } else { read($in, $buf, 50); $_ .= $buf; } } print $out $_; # spit out last chunk