Category: development tool
Author/Contact Info Artem Litvinovichartem@hoflink.com
Description: Finds specified substrings in documents under a specified directory. Usefull for tracking down obscure variables and methods/functions/subs in source packages.
#!/usr/bin/perl
# findstr by Artem Litivnovich
# November 1, 2001
# recursively searches for specified string within specified directory
# usage: findstr <directory> <string>

use File::Find;
my ($sep,$recdepth) = ('/', 3);

print "looking in $ARGV[0] for $ARGV[1]\n";
find(\&wanted, $ARGV[0]);

sub wanted {
    undef $/;
    open FILE, "<$_";
    my $text = <FILE>;
    print "Found $ARGV[1] in $File::Find::name\n" if($text =~ /$ARGV[1
+]/);
    close(FILE);
}
Replies are listed 'Best First'.
Re: findstr
by gmax (Abbot) on Dec 28, 2001 at 13:31 UTC
    Some comments.
    1. You should use the <CODE> and </CODE> tags to surround your code.
    2. Brackets around $ARGV subscripts are missing and in line 16 your code should be my $text = <>;
    3. Slurping the whole file at once (undef $/;) might not be the best solution for efficiency if you read a 500 MB file and your RAM is only 128 MB.
    gmax
Re: findstr
by dmitri (Priest) on Jan 04, 2002 at 09:54 UTC
    No offense, but this is called reinventing the wheel.

    bash$ grep -r func_name /home/user/source-tree

    You can use -i option to make the pattern case-insensitive. In short, man grep.