use strict; use warnings; my $fname = 'test.dat'; my $csize = 102400; my $word = 'bingo'; my ($handle, $length, $end, $pos, $c); open($handle, $fname); while () { $length = read($handle, $_, $csize); if (index($end.$_, $word) != -1) { $pos = tell($handle) - $length + index($end.$_, $word) - 100; $pos = 0 if $pos < 0; seek($handle, $pos, 0); read($handle, $_, 220); for (@_ = split(/\W+/)) { if ($_ eq $word) { print join(' ', @_[(($c > 4) ? $c - 5 : 0)..(($c < $#_ - 3) ? $c + 5 : $#_)]); last; } $c++; } last; } last if (!$length); ($end) = m/[\W](\w*$)/; } close($handle);