Need help to extract begin & end of date in below date & timestamps list
let say I have below timestamps data extracted from log file and need to extract only begin & end for each date & time .
The output will be use for time difference between begin to end calculation.
Data
2021-Oct-11 16:27:39
2021-Oct-11 16:28:10
2021-Oct-11 16:30:47
2021-Oct-11 16:30:47
2021-Oct-12 09:27:03
2021-Oct-12 10:27:03
2021-Oct-12 18:22:15
2021-Oct-13 08:57:16
2021-Oct-13 08:57:37
2021-Oct-13 11:33:25
2021-Oct-13 11:33:25
2021-Nov-08 16:45:17
2021-Nov-08 16:49:10
2021-Nov-08 17:00:50
2021-Nov-08 22:10:24
2021-Nov-09 11:51:35
2021-Nov-09 11:52:40
2021-Nov-09 12:14:39
2021-Nov-09 14:23:10
...
...
...
expected Output
begin : 2021-Oct-11 16:27:39
end :2021-Oct-11 16:30:47
begin : 2021-Oct-12 09:27:03
end :2021-Oct-12 18:22:15
begin :2021-Oct-13 08:57:16
end :2021-Oct-13 11:33:25
begin :2021-Nov-08 16:45:17
end :2021-Nov-08 22:10:24
begin :2021-Nov-09 11:51:35
end :2021-Nov-09 14:23:10
below are my script, but I only managed to extracted the begin data only
%key;
while ( <IN> ) {
$_=~ s/^\s |\s $//g;
my ($saved_date, $record_date);
chomp ;
my ($k, $d) = split;
if (! exists $key{ $k } ) {
$key{ $k } = $d;
my ($dtformat) = $d =$dtformat;
$end_date = Time::Piece->strptime($_, $dtformat);
next;
my ($dtformat) = $key{ $k } = $dtformat;
$saved_date = Time::Piece->strptime($_, $dtformat);
my ($dtformat) = $d =$dtformat;
$record_date = Time::Piece->strptime($_, $dtformat);
if ( $record_date - $saved_date > 0 ) {
$key{ $k } = $d;
}
}
}#endofwhile
for ( sort keys %key ) {
print "begin: ", $_, " ",$key{ $_ },"\n";
}
Output
begin: 2021-Nov-08 16:45:17
begin: 2021-Nov-09 11:51:35
begin: 2021-Oct-10 21:59:39
begin: 2021-Oct-11 00:21:46
begin: 2021-Oct-12 09:27:03
begin: 2021-Oct-13 08:57:16
begin: 2021-Sep-30 00:21:23
need help how to get the "end" part
thanks
CodePudding user response:
The secret is in the data structure you've used to store your data. As you're interested in the first and last timestamps for a day, I've built a hash keyed on the date where the value is an array containing all of the timestamps for that day. We can then sort the timestamps for each day and easily extract the earliest and latest ones.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use Time::Piece;
# The hash where we'll store our data
my