一个按照包含字符串为条件搜索文件的perl程序
目录
一个按照包含字符串为条件搜索文件的perl程序
#! perl -w
use File::Find;
if ($#ARGV != 0) {
print "Invalid Usage:\n";
print "Usage: perl browse_string.pl <str>\n";
exit;
}
$searchStr = shift @ARGV;
@files = ();
@directories_to_search = (".");
find(\&wanted, @directories_to_search);
foreach $file (@files) {
if (-d $file) {
}
else {
open FILE, "<".$file or die "cannot open file ".$file."\n";
while (<FILE>) {
if (/$searchStr/) {
print "$file\n";
last;
}
}
close FILE;
}
}
sub wanted {
@files = (@files, $File::Find::name);
}
用法:“perl program.pl
搜索目录及子目录,如果文件中包含该字符串,则打印该文件。