zoukankan      html  css  js  c++  java
  • Perl初级教程 (5) 遍历文件夹内指定扩展名文件,查找匹配关键字的输出。

    #!/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程序所在的全路径。

    - Make people around you successful is the biggest contribution to ourselves. -

  • 相关阅读:
    Milking Time---poj3616(简单dp)
    elasticsearch-入门(一)
    Spring Cloud Sleuth(十四)
    Spring Cloud Stream(十三)
    Spring Cloud-Bus(十二)
    Spring Cloud-config(十一)
    mac Gitblit安装
    git学习笔记
    java陷阱之spring事物管理导致锁无效
    Spring Cloud-Zuul(十)
  • 原文地址:https://www.cnblogs.com/zencorn/p/2608195.html
Copyright © 2011-2022 走看看