Brian wrote:
John W. Krahn wrote:
Brian wrote:

I would like to do a recursive search of directories and files, checking to see if a file contains a certain string.
<STDIN> will be a string containing whitespace(s).
As soon as I hit enter, I would like dummy.txt to be time stamped and again at termination.
I'm playing about with 9.85GB (2.57 million files).

If possible, full pathnames of the files containing the string to go to dummy.txt

I would like to see just how quickly PERL can complete a search, using Windows search facility I killed the incomplete search after leaving it to run for almost a day.

You could try this and see how it does:

#!/usr/bin/perl
use warnings;
use strict;
use File::Find;

open my $REPORT, '>', 'dummy.txt' or die "Cannot open 'dummy.txt' $!";
print $REPORT scalar localtime, "\n";

print STDERR 'Enter a string to search for: ';
chomp( my $string = <STDIN> );

find sub {
    return unless -f;
    open my $FH, '<:mmap', $_ or die "Cannot open '$_' $!";
    local $/;
<$FH> =~ /\Q$string/ && print $REPORT "$File::Find::name\n" and return;
    }, '/path/to/search/from';

print $REPORT scalar localtime, "\n";

__END__

Unknown PerlIO layer "mmap" at mysearch.pl line 14, <STDIN> line 1.

I thought that Windows supported memory mapping, oh well.

Cannot open file "dummy.txt No such file or directory at mysearch.pl line 14 <STDIN> line 1.

the script is placed under C:
and I replaced '/path/to/search/from/' with '/test/'

There is no string '/path/to/search/from/' in the example I posted, there is however the string '/path/to/search/from'.

(in "Windows-speak" test's pathname =  C:\test )

If all the files are small enough to fit into memory you could change '<:mmap' to '<:raw'.

If the files and search string are "standard" text files then you could replace:

    open my $FH, '<:mmap', $_ or die "Cannot open '$_' $!";
    local $/;
    <$FH> =~ /\Q$string/ && print $REPORT "$File::Find::name\n" and return;

With:

    open my $FH, '<', $_ or die "Cannot open '$_' $!";
    while ( <$FH> ) {
        /\Q$string/ && print $REPORT "$File::Find::name\n" and return;
        }

If the files you have to search through are "binary" and fairly large then you could use some sort of "sliding window" buffer.



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to