in reply to Re: Searching through text files
in thread Searching through text files

Hi dragonchild,

thanks for your prompt response. I don't have an answer on how efficient it needs to be, I just want to know the quickest way to do the task. I guess all I was really looking for was the quickest technique for doing this task using perl.

Thanks a lot
Jonathan

Replies are listed 'Best First'.
Re: Re: Re: Searching through text files
by calin (Deacon) on Mar 23, 2004 at 19:04 UTC

    Below is a simple solution with little perl code and relying on standard UNIX tools (find and grep). grep -F is used for fast (fixed string instead of regexp) search. It searches all the files in $DIR and all its subdirectories and obtains the names of those matching $STRING. Note that Perl 5.8.0+ is required (for the safe version of open). If you don't have it you must do the shell escaping yourself.

    #!/usr/bin/perl # safe form of IPC open requires perl 5.8.0 use v5.8.0; my $DIR = '/some/directory'; my $STRING = 'hidden!'; open my $fh, '-|', 'find', $DIR, qw/-type f -exec grep -lF/, $STRING, +qw/{} ;/ or die $!; chomp (my @found = <$fh>); # @found now contains the list of files matching the string;