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=<FileHandle>)) {
$back=index($line,$searchstr);
if ( $back != -1 ) {
print”$file:$lines:$line”; #打印提取到的行
}
$lines=$lines+1;
}
}
close(FileHandle);
}
closedir(DIRHANDLE);

    原文作者:Waste_Land
    原文地址: https://www.jianshu.com/p/00a69da64576
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞