Perl 搜索文件夹下文件所包含字符串

$searchdir="c:/folder"; #指定根文件夹路径
chomp $searchdir;
$searchstr="N2O_emission"; #指定欲提取的字符串
chomp $searchstr;
opendir(DIRHANDLE,$searchdir) || die "Cann't open $searchdir !";
while($file=readdir DIRHANDLE) {
if (-d "$searchdir/$file") {
printf "$searchdir/$file is a directory!\n";
} else {
open(FileHandle, "$searchdir/$file") || die "cann't open $searchdir/$file !\n";
$lines=1;
while (defined($line=)) {
$back=index($line,$searchstr);
if ( $back != -1 ) {
print"$file:$lines:$line"; #打印提取到的行
}
$lines=$lines+1;
}
}
close(FileHandle);
}
closedir(DIRHANDLE);

你可能感兴趣的:(Perl 搜索文件夹下文件所包含字符串)