特定の日付のファイルを一覧表示し、Perlで別のファイルに書き込む

特定の日付のファイルを一覧表示し、Perlで別のファイルに書き込む

リストがあります。.DTA次の日付のファイルです。

Jun 26 02:53 fs261803.DTA
Jun 26 02:54 fs261804.DTA
Jun 26 02:56 fs261805.DTA
Jun 26 03:25 fs261865.DTA
Jun 26 03:27 fs261869.DTA
Jun 27 03:21 fs271865.DTA
Jun 27 03:23 fs271869.DTA
Jun 28 03:23 fs281865.DTA
Jun 28 03:25 fs281869.DTA
Jun 29 03:21 fs291865.DTA
Jun 29 03:23 fs291869.DTA
Jun 29 03:54 fs291803.DTA

全部書きたい6月29日データをファイルとして保存します。複数のファイルを1つにマージするロジックがあります。以下は私のコードです。

#!/usr/bin/perl

use strict;
use warnings;
use feature 'say';

my $start_pos = 0;
my $data_len  = 15;
my $pad_len   = 54;

my $dir = ".";
opendir(DIR, $dir) or die "Cannot open directory: $dir!\n";
my @files = readdir(DIR);
closedir(DIR);
open my $out, ">>output.trns" or die "Cannot open output.trns!\n";

foreach my $file (@files)
{
    if($file =~ /DTA$/)
        #if ($file=`ls -rlt *.DTA` )
    {
        #print "$file\n";
        open my $in, "<$file" or die "Cannot open $file!\n";
        while (<$in>) {
        say $out ' ' x $pad_len, substr($_, $start_pos, $data_len);
        }

        close $in;
        }
}
close $out;

このコードでは、すべての項目を組み合わせることができます。.DTAファイルが1つにまとめられます。しかし、ただ合わなければなりません。6月29日ファイルを使用して新しいファイルを作成します。誰でも私を助けることができますかperl

ベストアンサー1

ファイルのタイムスタンプ(修正時刻による見積もり)を比較するには、stat()各ファイルをインポートして年、月、日を比較する必要があります。以下は、2018-06-29のフィルタをハードコードする例です。

next unless $file =~ /DTA$/;
my $fullpath = $dir . "/" . $file;
my $wantedyear=2018;
my $wantedmonth=5; ## zero-based
my $wantedday=29;
my $mtime=(stat $fullpath)[9];
my $fileyear=(localtime($mtime))[5] + 1900;
next unless $fileyear == $wantedyear;
my $filemonth=(localtime($mtime))[4];
next unless $filemonth == $wantedmonth;
my $fileday=(localtime($mtime))[3];
next unless $fileday == $wantedday;

おすすめ記事