Home > Back-end >  Get timestamps between 8am to 5pm from timestamps list
Get timestamps between 8am to 5pm from timestamps list

Time:12-13

I have a list of extracted date timestamps. From this list I want to print timestamps between 8AM to 5PM foreach date/time. But my code below does not produce any output.

DATA:

2021-Sep-16 21:24:48
2021-Sep-17 03:31:05
2021-Sep-17 08:30:23
2021-Sep-17 09:42:43
2021-Sep-17 12:43:14
2021-Sep-17 14:43:23
2021-Sep-17 15:50:34
2021-Sep-17 16:50:35
2021-Sep-18 03:31:05
2021-Sep-18 08:30:23
2021-Sep-18 09:42:43
2021-Sep-18 12:43:14
2021-Sep-18 14:43:23
2021-Sep-18 15:50:34
2021-Sep-18 22:50:35

Expected output:

2021-Sep-17 08:30:23
2021-Sep-17 09:42:43
2021-Sep-17 12:43:14
2021-Sep-17 14:43:23
2021-Sep-18 08:30:23
2021-Sep-18 09:42:43
2021-Sep-18 12:43:14
2021-Sep-18 14:43:23

My code:

sub read_timestamps{
    my $file = './logs/file.log';
    open(IN, "<" ,"$file") or die "Couldn't open file $!";
    open(OUT, ">" ,"_trash/tmp/raw/0-out.txt") or die "Couldn't open file $!";

    my @content = <IN>;
    my $start="08:00:00";
    my $end = "17:00:00";

    foreach $lines(@content){
        if($lines = ~/$start/../$end/){
             print OUT ("$1 \n") if($lines =~ /(\d{4}-\w -\d{2} \d\d:\d\d:\d\d)/);
        }
    } 
}

CodePudding user response:

while (<>) {
   my ( $h ) = / (\d )/
      or next;

   print if $h >= 8 and $h < 17;
}
  • Related