#!/usr/bin/perl -w use strict; my $logfile = "/path/to/logfile"; my $outfile = "/path/to/outfile"; my $find = "hello"; my $second = ''; my $first = ''; my $line = 0; # allow regex unfriendly chars in $find $find = quotemeta $find; open (FILE, "<$logfile") or die "Oops perl says $!"; while () { chomp; $line++; &print_found if /$find/; $second = $first; $first = $_; } sub print_found { open (OUT, ">>$outfile") or die "Oops perl says $!"; print OUT "Line: $line\n"; print OUT "$second\n$first\n$_\n\n"; close OUT; }