#! perl -slw
use strict;
use Time::HiRes qw[ time ];
sub slurp {
local( $/, @ARGV ) = ( -s( $_[0] ), $_[0] );
<>;
}
my $start = time;
my $user = $ARGV[0] or die 'No user supplied';
my @matches;
my $files = 0;
## For each matching file in the Hist directory
while( my $file = glob 'hist/*.htm' ) {
$files++;
## Slurp the contents
my $contents = slurp $file;
## And extract the comments
next unless $contents =~ m[ (.+) ]six;
my $comments = $1;
## Break out each individual comment
while(
$comments =~ m[
( ## Capture
\n \s*
## from the
.+? ## Everything (non-greedy)
)
(?= \n \s*
) ## Up to the next
]gsix
) {
my $comment = $1;
## And save it if it contains the specified user name
push @matches, "$file\n$comment" if $comment =~ m[
\n \s* ## On a line, possible leading whitespace
\Q$user\E ## The user name
[^\n]*
## maybe other (non-newline) stuff
\s* \n ## maybe whitespace and newline
]mxi;
}
}
printf "Searched $files files in %g seconds\n", time() - $start;
die "No match found for user $user" unless @matches;
print "User $user found in files:\n-------\n", join "\n---------\n", @matches;
__END__
P:\test>522029 Buk
Searched 133 files in 0.144615 seconds
No match found for user Buk at P:\test\522029.pl line 49, <> line 133.
P:\test>522029 Doug
Searched 133 files in 0.147709 seconds
User Doug found in files:
-------
hist/h0511.htm
Comment 1
Doug <hun@tele.com>
USA - Thu 11/29/2005 - 22:05:51
---------
hist/h0601.htm
Comment 1
Doug <hun@tele.com>
USA - Thu 01/05/2006 - 22:05:51
P:\test>522029 "J H"
Searched 133 files in 0.149665 seconds
User J H found in files:
-------
hist/h0511.htm
Comment 2
J H
Clearwater, FL USA - Wed 01/04/2006 - 02:05:12
---------
hist/h0601.htm
Comment 2
J H
Clearwater, FL USA - Wed 01/04/2006 - 02:05:12