加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl初级教程 (5) 遍历文件夹内指定扩展名文件,查找匹配关键

发布时间:2020-12-16 00:18:56 所属栏目:大数据 来源:网络整理
导读:#!/usr/bin/perl -W # # File: find.pl # License: GPL-2 use strict; use warnings; use File::Find; ? #定义要匹配的关键字 my $str1="20090414095014"; my $str2="Report"; #定义键盘接收输入,第一个为文件夹路径,第二个为文件扩展名。 die "Usage: $0

#!/usr/bin/perl -W
#
# File: find.pl
# License: GPL-2

use strict;
use warnings;
use File::Find;

?

#定义要匹配的关键字


my $str1="20090414095014";
my $str2="Report";

#定义键盘接收输入,第一个为文件夹路径,第二个为文件扩展名。

die "Usage: $0 <dir> <extion>n" unless @ARGV == 2;
my $Dir = $ARGV[0] ;
my $Ext = $ARGV[1] ;

opendir(DH,"$Dir") or die "Can't open: $!n" ;
?

my @list = grep {/$Ext$/ && -f "$Dir/$_" } readdir(DH) ;
closedir(DH) ;
chdir($Dir) or die "Can't cd dir: $!n" ;

#遍历文件夹,定义文件句柄。
foreach my $file (@list){
??? open(FH,"$file") or die "Can't open: $!n" ;
??? while(<FH>){

# Perl 本身是大小写区别的,而且对于关键字搜索是借助于正则表达式来完成的。??if(<FH> =~ /$str2/i){????print "$file:n" ;????}??? }??? print "n";??? close(FH) ;}# 在CMD模式下调用时切记使用perl程序所在的全路径。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读