Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
perl读取多个文件_Perl - Fatal编程技术网

perl读取多个文件

perl读取多个文件,perl,Perl,在Perl中 如何一次读取多个文件 我在月底收到了一份评级报告 每天将创建一个日志文件 我想看整个月的档案 我的文件类似于这样的t.log.mmddyyy您可以 readdir以获取目录中的文件列表 分析每个文件名以确保它与您的格式匹配 打开并读取每个文件 该函数将允许您检索与特定模式匹配的文件名列表。如果将该列表加载到@ARGV,则可以处理所有文件,即使是以单循环的顺序: use strict; use warnings; use Getopt::Long; sub usage ($) {

在Perl中

如何一次读取多个文件

我在月底收到了一份评级报告

每天将创建一个日志文件

我想看整个月的档案

我的文件类似于这样的t.log.mmddyyy

您可以

  • readdir以获取目录中的文件列表
  • 分析每个文件名以确保它与您的格式匹配
  • 打开并读取每个文件
该函数将允许您检索与特定模式匹配的文件名列表。如果将该列表加载到
@ARGV
,则可以处理所有文件,即使是以单循环的顺序:

use strict;
use warnings;
use Getopt::Long;

sub usage ($) { 
    my $msg = shift;
    die <<"END_MSG";
*** $msg
Usage: $0 --month=nn --year=nn PATH

END_MSG
}

GetOptions( 'month=i' => \my $month, 'year=i'  => \my $year );

usage "Month not specified!" unless $month;
usage "Year not specified!"  unless $year;
usage "Invalid month specified: $month" unless $month > 0 and $month < 13;
usage "Invalid year specified: $year"   unless $year  > 0;

my $directory_path = shift;
die "'$directory_path' does not exist!" unless -d $directory_path;

@ARGV = sort glob( sprintf( "$directory_path/t.log.%02d??%02d", $month, $year ));
while ( <> ) { # process all files 
    ...
}
使用严格;
使用警告;
使用Getopt::Long;
子用途($){
我的$msg=shift;
死亡\我的$year);
用法“未指定月份!”除非$Month;
用法“未指定年份!”除非$Year;
用法“指定的月份无效:$month”,除非$month>0且$month<13;
用法“指定的年份无效:$year”,除非$year>0;
我的$directory\u path=shift;
死“'$directory\u path'不存在!”除非-d$directory\u path;
@ARGV=sort glob(sprintf($directory_path/t.log.%02d???%02d',$month,$year));
while(){#处理所有文件
...
}

首先:它是“Perl”。它不是首字母缩略词。其次,你尝试了什么,你有什么样的数据?请阅读关于如何提问的常见问题解答。但dircotry包含全年,但我想阅读Permont文件,就像,月底,我会发送报告,这样,我想一次阅读所有日志文件