#! perl -slw use strict; open I, '<:raw', $ARGV[0] or die $!; my $regex = $ARGV[1] or die 'No search pattern supplied.'; my $o = 0; my $buffer; ## Read into the buffer after any residual copied from the last chunk while( my $read = read I, $buffer, 4096, pos( $buffer )||0 ) { while( $buffer =~ m[$regex]gc ) { ## Print the offset, the matched text plus (following) context print $o + $-[0], ':', substr $buffer, $-[0], 100; } ## Slide the unsearched remainer to the front of the buffer. substr( $buffer, 0, pos( $buffer ) ) = substr $buffer, pos( $buffer ); $o += $read; ## track the overall offset. } close I;