in reply to Searching word(s) in multiple file and directories
And the output -use strict; use warnings; use File::Find; use Getopt::Long; use Pod::Usage; use Carp; # Parse command line arguments and assign corresponding variables GetOptions ( 'r|rootdir=s' => \( my $rootdir = "./" ), 'p|pattern=s' => \( my $pattern = undef ), 'i' => \( my $case_sensitive = 0 ), 'v|verbose' => \( my $verbose = 0 ), ); unless ( defined $pattern ) { print <<USAGE Usage: $0 [options] Options: -r|--rootdir [dir] Specify the top directory -p|--pattern [pattern] Specify the pattern to look for (regex) -i Case sensitive search -v|--verbose Print more info USAGE ; exit(0); } $pattern = "(?i)" . $pattern if $case_sensitive; print "looking under $rootdir for { /$pattern/ }\n" if $verbose; find({ wanted => \&filefilter }, $rootdir); sub filefilter { return if /^\.+$/; open FILE, "<$File::Find::name" or carp "could not open file: $File::Find::name"; my $file = do { local $/; <FILE> }; close FILE; print ">> ", $File::Find::name, "\n" if $file =~ /$pattern/; }
P:\Perl>perl search.pl -r ./ -p "(Find|Dir)" -i >> ./f1.pl >> ./f15.pl >> ./f2.pl >> ./f48.pl >> ./f6.pl >> ./fun1.pl >> ./Fun2.pl >> ./result.txt >> ./search.pl
|
|---|