aah, that was a fun diversion! i searched for 'bob'. you might like to search for something more useful....#!/usr/bin/perl -w use strict; my $ext = '.log'; # extension to look for in filenames my $pat = 'bob'; # adjust to value to search for my $match; # track how many matches opendir(DH, '.') or die("CANT! $!"); # open directory foreach my $diritem ( readdir(DH) ) { # read directory next unless ( -f $diritem && # is it a file? $diritem =~ m/$ext$/ ); # does it end in $ext? open(FH, '<', $diritem) or die("CANT! $!"); # open the file for re +ading my @buffer; # buffer for previous lines push @buffer, scalar <FH> for 1 .. 3; # create three line bu +ffer while(<FH>) { # read the file line by line push @buffer, $_; # add line to end of buffer shift @buffer; # remove line from front of buff +er if( /$pat/ ) { # did i find the search pattern? $match++; # increment my match count # print fancy output, with # separator line, # match counter, filename, # two previous lines and matching line print "-----------------------------------------\n"; print "($match) From file $diritem\n"; print $buffer[0], $buffer[1], $buffer[2]; } # if } # while close FH; # close the file } # foreach
~Particle
In reply to Re: Newbie Text Parsing Question
by particle
in thread Newbie Text Parsing Question
by kanikilu
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |