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

Hello Monks ,, I wonder if there is an easy way to do this in perl .
I have 100 dirctory under /main/w01 /main/w02 /main/w03 . . /main/w100
I want a way to grep inside all these dirctories and find out if the word ORA in one of the files in side those directories. Under each directory , there is lots of file and some of them might contains ORA error , I need to find out which directories are they. thanks

Replies are listed 'Best First'.
Re: greping for a word
by thor (Priest) on Apr 29, 2004 at 03:38 UTC
    Here's a quickie:
    use warnings; use strict; use File::Find; my $search_string = "ORA"; find( \&grep, "." ); sub grep { my $file = $File::Find::name; if ( -f $file && -r _ ) { open( my $fh, "<", $file ) or return; while ( my $rec = <$fh> ) { print "Found $search_string in $file" if $rec =~ /$search_string/; } } }
    However, if you have access to Unix, then the following shell snippet might be more useful in the long run:
    find . -type f | xargs grep -l ORA

    thor

      xargs? blech.
      find . -type f -exec grep -l ORA {} \;

        [Inevitable off-topic discussion ensues]

        With a large amount of files (and with hundreds of directories, we can assume there are a large amount of files. Either that or someone has designed a very bad filesystem layout), using -exec with find can be substantially slower than using xargs. The reason being that -exec forks once for every file, whereas xargs will pack as many arguments on a single command line as possible. This is not only conceptually cleaner (imho), but like I said, with lots of files it's noticeably faster.

        Update: Just to test my assertions, I ran a test to see what the difference was. As you can see, the directory I'm running this in has 29,388 files. Granted this benchmark might not be representative of the actual post, but the difference is quite dramatic. Maybe I've done something wrong:

        $ time find ./ -type f | wc -l 29388 real 0m0.244s user 0m0.034s sys 0m0.181s $ time find ./ -type f -print0 | xargs -0 ls | wc -l 29388 real 0m1.049s user 0m0.420s sys 0m0.588s $ time find ./ -type f -exec ls '{}' \; | wc -l 29388 real 0m46.239s user 0m16.509s sys 0m25.974s

        I ran each test multiple times, and posted the lowest time for each.

Re: greping for a word
by Zaxo (Archbishop) on Apr 29, 2004 at 03:27 UTC

    File::Find will assist in a recursive search over a directory tree. Your sub wanted will need to open each ordinary file and look for the match m/ORA/ in its content. If found, the filename can be pushed onto an array, or whatever.

    After Compline,
    Zaxo

Re: greping for a word
by jacques (Priest) on Apr 29, 2004 at 03:32 UTC
    # grep -i ora `find /main/w* -type f` > errors

      Since we're doing unix commands, here's how I'd do it:

          grep -ri ora /main/w*

      I'm not sure if -r is a GNU-only switch, but it's very handy. :)