This does the trick with inplace editing - lets you do really big files without a really big memory :-)
#!/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 (<FILE>) { 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; }
Hope this helps
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
In reply to Re: Newbie Text Parsing Question
by tachyon
in thread Newbie Text Parsing Question
by kanikilu
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |