===> I  am using the following regex :

([A-Z][a-z]{2}\s)([0-9]{2}\s[0-2][0-9](:[0-5][0-9]){2}\s[0-9]{4})

> Both are working as expected I would like to know if these are good regex
or it can be better , please suggest .

Concurring with the others, your setting yourself up for trouble with the
RE route (famous saying: "So you have a programming problem and say, 'I
know, I solve it with a regex!' Now you have two problems"). As I
mentioned, Perl gives you epoch time and localtime() or POSIX::strftime let
you turn that into nice human-readable strings.

But to your RE, well, besides assuming there'll always be just one space,
you're capturing the spaces (and separators), which means you'll probably
need to ditch them later.  Looking a little closer, I don't think you're
using the capture parens for capturing. You're RE is actually sort of
validating the timestamp, so it'll fail on a different one, say with all
lower case month or no prefix zero or something. For handling data from
outside sources, it usually better to do a broader match and then test the
parts for validity so you will know which part failed.

if ( $file_info_str =~ /(\w{3})\s+([0-9]{2})\s+(\d+):(\d+):(\d+)\s+(\d+)/ )
{
  my ($mon, $date, $hour, $min, $sec, $year) = ($1, $2, $3, $4, $5, $6);'
... [ work with time stamp ]
}
else {
    warn("unable to find time stamp: $file_info_string\n");
...

On Wed, Oct 24, 2018 at 9:49 AM Chris Fedde <ch...@fedde.us> wrote:

> I cannot emphasize enough how fragile the perhaps obvious regex based
> comparisons of timestamps can be. I second the approach demonstrated by
> Илья Рассадин above.  There are subtle and difficult to debug problems
> buried in timestamps.  Not least of which is locale ambiguity,
> discontinuities like daylight savings time, leap seconds, and more.  I
> second the recommendation to use Time::Piece.
> Doc is here: https://perldoc.perl.org/Time/Piece.html
>
>
> On Tue, Oct 23, 2018 at 11:42 PM Asad <asad.hasan2...@gmail.com> wrote:
>
>> Thank you all for the reply it is working for me .
>>
>> 1) for  02/23/18 01:10:33  ==> I  am using the following regex
>>
>> \d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]
>>
>> 2) Feb 23 01:10:28 2018
>>
>> ====> I  am using the following regex :
>>
>> ([A-Z][a-z]{2}\s)([0-9]{2}\s[0-2][0-9](:[0-5][0-9]){2}\s[0-9]{4})
>>
>>
>> Both are working as expected I would like to know if these are good regex or 
>> it can be better , please suggest .
>>
>> Thanks,
>>
>>
>>
>> On Tue, Oct 23, 2018 at 12:14 PM Asad <asad.hasan2...@gmail.com> wrote:
>>
>>> Hi All ,
>>>
>>>         first hurdle is how do I extract this Feb 23 01:10:28 2018  from
>>> file1 which regex can I use ?
>>>
>>>          convert it into epoch
>>>
>>>         then
>>>
>>>          regex for 02/23/18 01:10:33  is required  ?
>>>
>>>         convert into epoch
>>>
>>>        So if you can suggest the correct regex for both timestamps.
>>>
>>> Thanks,
>>>
>>> On Tue, Oct 23, 2018 at 12:11 PM Илья Рассадин <elcaml...@gmail.com>
>>> wrote:
>>>
>>>> use Time::Piece;
>>>>
>>>> my $t1 = Time::Piece->strptime('Feb 23 01:10:28 2018', '%b %d %H:%M:%S
>>>> %Y');
>>>>
>>>> my $t2 = Time::Piece->strptime('02/23/18 01:10:33', '%m/%d/%y
>>>> %H:%M:%S');
>>>>
>>>> if ($t1 > $t2) { ... }
>>>> On 23/10/2018 09:17, Asad wrote:
>>>>
>>>> Hi All ,
>>>>
>>>>         first hurdle is how do I extract this Feb 23 01:10:28 2018
>>>> from file1 which regex can I use ?
>>>>
>>>>          convert it into epoch
>>>>
>>>>         then
>>>>
>>>>          regex for 02/23/18 01:10:33  is required  ?
>>>>
>>>>         convert into epoch
>>>>
>>>>        So if you can suggest the correct regex for both timestamps.
>>>>
>>>> Thanks,
>>>>
>>>> On Tue, Oct 23, 2018 at 11:21 AM Asad <asad.hasan2...@gmail.com> wrote:
>>>>
>>>>> Thanks, I will do that. It was for perl .
>>>>>
>>>>> On Tue, Oct 23, 2018 at 10:42 AM Jim Gibson <jimsgib...@gmail.com>
>>>>> wrote:
>>>>>
>>>>>> On Oct 22, 2018, at 9:12 PM, Asad <asad.hasan2...@gmail.com> wrote:
>>>>>> >
>>>>>> > file1 :
>>>>>> > Patching tool version 12.1.0.2.0 Production on Fri Feb 23 01:10:28
>>>>>> 2018
>>>>>> >
>>>>>> > Bootstrapping registry and package to current versions...done
>>>>>> > statement ERR-2001: table is corrupt check for cause
>>>>>> >
>>>>>> > could not determine the current status.
>>>>>> >
>>>>>> > file2 :
>>>>>> >
>>>>>> >  LOG file opened at 02/03/18 01:11:05
>>>>>> >
>>>>>> > DUP-05004:   statement1
>>>>>> > DUP-05007:   statement2
>>>>>> >
>>>>>> >
>>>>>> >  LOG file opened at 02/03/18 01:11:14
>>>>>> >
>>>>>> > DUP-05004:   statement1
>>>>>> >
>>>>>> > DUP-05007:   statement2
>>>>>> >
>>>>>> >
>>>>>> >  LOG file opened at 02/23/18 01:10:33
>>>>>> >
>>>>>> > DUP-05004:   statement1
>>>>>> >
>>>>>> > DUP-05007:   statement2
>>>>>> >
>>>>>> > I need to look for the ERR-2001 in file1 if it matches then go to
>>>>>> file2 and print the message nearest to the timestamp found in file1 
>>>>>> within
>>>>>> two minutes of range .
>>>>>> >
>>>>>> > so in this case file1 :  Fri Feb 23 01:10:28 2018
>>>>>> >                    range   file1 +2 mins :02/23/18 01:12:28
>>>>>> > check in file 2 nearest to file1 and within range :     02/23/18
>>>>>> 01:10:33
>>>>>> >
>>>>>> > how do i compare two timestamps in different format and within
>>>>>> range  ?
>>>>>>
>>>>>> You would first convert the two timestamps to a common format,
>>>>>> preferably one that used a numerical value to express times. I know of 
>>>>>> two
>>>>>> such: the Unix epoch time that uses an integer to represent the number of
>>>>>> seconds since 1 Jan 1970 UTM and the Julian date that uses a 
>>>>>> floating-point
>>>>>> number to represent the number of days since 1 Jan 4713 BCE.
>>>>>>
>>>>>> Are you looking for a Perl solution or a Python one?
>>>>>>
>>>>>> For Perl, you should investigate time and date modules available on
>>>>>> CPAN, such as Date::Manip or Date::Calc.
>>>>>>
>>>>>>
>>>>>
>>>>> --
>>>>> Asad Hasan
>>>>> +91 9582111698
>>>>>
>>>>
>>>>
>>>> --
>>>> Asad Hasan
>>>> +91 9582111698
>>>>
>>>>
>>>
>>> --
>>> Asad Hasan
>>> +91 9582111698
>>>
>>
>>
>> --
>> Asad Hasan
>> +91 9582111698
>>
>

-- 

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk

Reply via email to