Hello again stray_tachyon,

As I said use File::Find::Rule and convert it to your way of fitting. I also prefer to use the module IO::All that will help you to load the file in an array so you can grep all the words in one go. Why I think this approach is better? You check only specific directories for specific files. You do not need to know the name of the file just the extension. So in conclusion for me filtering out the files that you need to open is more efficient.

From my point of view this is more efficient, maybe another Monk has a better idea. But at the end why to relay on people opinions when you simply can use Benchmark::Forking if you are on LinuxOS or WindosOS Benchmark.

Sample of code:

#!/usr/bin/perl use strict; use IO::All; use warnings; use Data::Dumper; use File::Find::Rule; my @LogDirs = ('/home/user/PerlMonks/', '/tmp/Monks'); # add more here my $level = shift // 3; # level to dig into my @files = File::Find::Rule->file() ->name( '*.txt', '*.log' ) #can insert regex too ->maxdepth($level) ->in(@LogDirs); my %hash; foreach my $file (@files) { my @lines = io($file)->chomp->slurp; my $matches = grep {/Start/ or /middle/ or /end/} @lines; $hash{$file} = $matches; } print Dumper \%hash; __END__ $ perl test.pl $VAR1 = { '/home/user/PerlMonks/Foo/Bar/monks.log' => 1, '/home/user/PerlMonks/sample.txt' => 1, '/tmp/Monks/SampleDir/monks.log' => 2 };

Sample of data in files:

$ cat /tmp/Monks/SampleDir/monks.log Start line key line. Ignore this line. Another line key end. $ cat /home/user/PerlMonks/Foo/Bar/monks.log This is line 1. This key line end. $ cat /home/user/PerlMonks/sample.txt This is another key line middle point. I do not care about this.

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re^3: How to efficiently search for list of strings in multiple files? by thanos1983
in thread How to efficiently search for list of strings in multiple files? by stray_tachyon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.