in reply to extract lines from file between date range.
Comparing dates/times can get complicated. In general, I recommend converting input strings to output strings in ISO format 3. This is like "YYYY-MM-DD" where leading 0's are required for single digit fields. The reason for doing this is that you can use a string comparison or simple sort rather than a more complex numeric sort. Here is some code for you. Note that I use string comparisons of "ge" (greater than or equal) and "le" (less than or equal).
Update: Minor code improvement to if statement, deleted $_ from print $_ as that is extraneous.
I don't know where the trailing, 22/03/2018 stuff came from? It appears that [03/Apr/2018:00:42:11 -0500] is the real time stamp. The -0800 means 8 hours behind GMT or UTC (perhaps Pacific Standard Time?). Yes, there is a fine difference between GMT and UTC that is meaningless here. Typically what would be done here is convert these times into UTC and use that for all DB storage. Convert to local time like perhaps Pacific Daylight Savings Time which is -0700 from GMT for user presentation.#!/usr/bin/perl use strict; use warnings; my $start ='2018-02-03'; #ISO 3 format YYYY-MM-DD my $end ='2018-02-06'; while (<DATA>) { if (my ($day, $month, $year) = $_ =~ m/(\d{2})\/(\d{2})\/(\d{4})\s* +$/) { my $this_date = "$year-$month-$day"; print if ($this_date ge $start and $this_date le $end); } } =prints K20TQ67 K1L0V T6Z148X 111.222.333.123 HOME - [03/Feb/2018:01: +08:39 -0800] 03/02/2018 SO78NZ28 B5S8J W9F920Z 111.222.333.123 HOME - [04/Feb/2018:01 +:08:39 -0800] 04/02/2018 BI55SY64 R6P5H A9U757R 111.222.333.123 HOME - [05/Feb/2018:01 +:08:39 -0800] 05/02/2018 ET43US76 F5S3W X2L870O 111.222.333.123 HOME - [06/Feb/2018:01 +:08:39 -0800] 06/02/2018 =cut __DATA__ some bogus line here DQ94JD84 S8G2H A9X946N 111.222.333.123 HOME - [01/Feb/2018:01 +:08:39 -0800] 01/02/2018 XA29EN35 M4C6M D7F577Q 111.222.333.123 AWAY - [02/Feb/2018:01 +:08:39 -0800] 02/02/2018 JK20TQ67 K1L0V T6Z148X 111.222.333.123 HOME - [03/Feb/2018:01 +:08:39 -0800] 03/02/2018 SO78NZ28 B5S8J W9F920Z 111.222.333.123 HOME - [04/Feb/2018:01 +:08:39 -0800] 04/02/2018 BI55SY64 R6P5H A9U757R 111.222.333.123 HOME - [05/Feb/2018:01 +:08:39 -0800] 05/02/2018 MH72RG27 Y6X0N C7E352J 111.222.333.123 HOME - [01/Feb/2018:01 +:08:39 -0800] 01/02/2018 ET43US76 F5S3W X2L870O 111.222.333.123 HOME - [06/Feb/2018:01 +:08:39 -0800] 06/02/2018 QA43MM82 Y3K0O H8E229P 111.222.333.123 HOME - [11/Feb/2018:01 +:08:39 -0800] 11/02/2018 MK72HI41 A6W2I M3X402D 111.222.333.123 HOME - [15/Feb/2018:01 +:08:39 -0800] 15/02/2018 LJ28XY72 E0Z4L E8W757S 111.222.333.123 HOME - [22/Mar/2018:01 +:08:39 -0800] 22/03/2018 TJ47EG77 W5J6A A7L557Q 111.222.333.123 HOME - [01/Feb/2018:01 +:08:39 -0800] 01/02/2018 TJ47EG78 E6L8Z J4X329P 216.142.233.73 - - [30/Nov/2018:00:03: +08 -0500] 30/11/2018 TJ47EG79 N2V1H Q1Y615G 10.101.128.66 - - [03/Apr/2018:00:42:1 +1 -0500] 03/04/2018
|
|---|