Bharath666 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have the pattern something like "keyword : Multinode". Now, I need to search this pattern in all the files in a directory. If we found the pattern in any of the file, a non empty-string should be returned. It may contain file-name or directory name

In shell scripting the following will do the same

KeyMnode=`grep -w "keyword : Multinode"  ${dirname}/*`

please tell me if you need any more information.Thanks

Replies are listed 'Best First'.
Re: pattern search in all the files in a directory
by tobyink (Canon) on Feb 19, 2013 at 08:14 UTC

    I really like Path::Class::Rule for searching the filesystem. Its author (David Golden) has recently been working on a faster replacement though (Path::Iterator::Rule) and your question has given me a chance to experiment with it...

    use 5.010; use strict; use warnings; use Path::Iterator::Rule; use Path::Tiny; # The directories we're looking in. # my @search_dirs = ('/home/tai/tmp'); # The regular expression we're searching for. # my $search_for = qr{foo}; "Path::Iterator::Rule"->add_helper( contents_match => sub { my $regexp = shift; sub { my $item = path(shift); return unless $item->is_file; $item->slurp_utf8 =~ $regexp; }; }, ); "Path::Iterator::Rule"->add_helper( line_match => sub { my $regexp = shift; sub { my $item = path(shift); return unless $item->is_file; my $fh = $item->openr_utf8; while (my $line = <$fh>) { return 1 if $line =~ $regexp; } return; }; }, ); my @matches = "Path::Iterator::Rule" -> new -> file -> name(qr{\.txt$}) -> line_match($search_for) -> all(@search_dirs) ; say for @matches;
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: pattern search in all the files in a directory
by 2teez (Vicar) on Feb 19, 2013 at 06:41 UTC

      Actually I am not able to take an exact move at this point. I thought of using find(subroutine,directory_path) and inside the sub-routine I want to traverse through the entire directory for all its entries. For every entry I want to put a check whether it is a readable file or not. If the file is readable, I want to search for the required pattern "keyword : Multinode" in the file found. If we hit with a success, the entire find command should result in a non-empty string(preferably only the existing directory Name) otherwise with an empty string. Please let me know if you need any further information

        Actually I am not able to take an exact move at this point Why is that?
        I thought of using find(subroutine,directory_path) That is a way to go. Try it out.
        If you could describe what you want like you did above, it shows you could make it happen. Try it out and you would be glad you did.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
Re: pattern search in all the files in a directory
by toolic (Bishop) on Feb 19, 2013 at 14:10 UTC