Date Range Problem

I have a log file that has the first few characters of each line as a timestamp.

2010-06-01 04: 56: 02 802 DEBUG {Thread-27} Some text message

2010-06-01 04: 56: 02 802 DEBUG {Thread-27} Some text message

2010-06-01 04: 56: 02 802 DEBUG {Thread-27} Some text message

2010-06-01 04: 56: 02 802 DEBUG {Thread-27} Some text message

2010-06-01 05:22: 02,802 DEBUG {Thread-27} Some text message

2010-06-01 05:22: 02,802 DEBUG {Thread-27} Some text message

2010-06-01 05:22: 02,802 DEBUG {Thread-27} Some text message

2010-06-01 05:22: 02,802 DEBUG {Thread-27} Some text message

2010-06-01 06: 43: 02 802 INFO {Thread-27} Some text message

2010-06-01 06: 43: 02 803 INFO {Thread-27} Some text message

2010-06-01 06: 43: 02 804 INFO {Thread-27} Some text message

2010-06-01 06: 43: 02 804 INFO {Thread-27} Some text message

2010-06-01 06: 43: 02 809 DEBUG {Thread-27} Some text message

2010-06-01 06: 43: 02 809 DEBUG {Thread-27} Some text message

2010-06-01 06: 43: 02 809 DEBUG {Thread-27} Some text message

2010-06-01 07: 08: 02,809 DEBUG {Thread-27} Some text message

2010-06-01 07: 08: 02,809 DEBUG {Thread-27} Some text message

My goal is to find all such lines that have a timestamp 1 hour before the current time.

How can this be achieved?

+3
3

DateTime :

use strict;
use warnings;
use DateTime;

my $oneHourAgo = DateTime->now()->subtract( hours => 1 );
my $threshold  = join ' ', $oneHourAgo->ymd, $oneHourAgo->hms;  # Time as string

open my $logFile, '<', 'logfile.txt';

while (my $log = <$logFile>) {

    chomp $log;
    my ($time) = split /,/, $log;       # Gets current log time

    print $log if $time ge $threshold;  # String-compares log time to threshold
}

close $logFile;
+4
+1

? , File:: ReadBackwards. , , . , , . (, .)

0

Source: https://habr.com/ru/post/1747930/


All Articles