如何过滤文件选择

问题描述 投票:0回答:1

我有以下表格的文件

wrfout_d01_2024-02-08_01_00_00
wrfout_d01_2024-02-08_02_00_00
wrfout_d01_2024-02-08_03_00_00
...

其中最后一个非零数字是该月的日期。我只想选择第 1 到 10 个或第 11 到 20 个文件。使用下面的代码选择 11 到 20,需要 1 -20。

while (my $file=readdir $DIR){
 next if -d $file;
 if($file =~ /^wrfout_$Dom[$kk]/){
  $iday=substr($file, 19,2);
  for($iday >= 11 && $iday <= 20){
   next unless($file =~ /^wrfout_$Dom[$kk]_\d{4}-\d{2}-$iday/);
   copy("$INPUT_DIR[$kk]/$file","$DATA_DIR") or die "Copy failed: $!";
  }
 }
}

所有变量@INPUT_DIR、$DATA_DIR、$DIR 都在其他地方定义。 如何修改我的代码以做出正确的选择?

perl
1个回答
1
投票

for($iday >= 11 && $iday <= 20)
应该是
if($iday >= 11 && $iday <= 20)

您还可以同时验证文件名提取日期:

示例:

while (my $file=readdir $DIR) {
    next if -d $file;
    # validate the beginning of the filename and extract the day in one go:
    if ($file =~ /^wrfout_$Dom[$kk]_\d{4}-\d{2}-(\d{2})/) {
#                                               ^     ^
#                                               capture

        $iday = $1;                         # captured in the above match

        if ($iday >= 11 && $iday <= 20) {   # if instead of for
            copy("$INPUT_DIR[$kk]/$file", $DATA_DIR) or die "Copy failed: $!";
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.