achak01 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks I have a log file which gives data in either of the following ways
Script /opt/OV/bin/OpC/agtinstall/inst.sh invoked by root at 02/25/11 14:36:48 <code> or <code> Script /opt/OV/bin/OpC/agtinstall/inst.sh invoked by root at 02/25/11 +14:36:48
I need to get the date time values. How do i proceed so whatever be the format, i can grep the date time value. Hello Monks. I have got it working Here is my code snippet
open(INFILE,"install.log")|| die("File not found"); while($line = <INFILE>) { if ($line =~ /((\d+)\/(\d+)\/(\d+))\Z/g) { $match = $&; $nextline = <INFILE>; $start="$match$nextline"; if($start =~ /(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+)(\d+)/) { print $4,"\n"; } } # end if if elsif ($line =~ /invoked by root at ((\d+)\/(\d+)\/(\d+))\s+(\d+): +(\d+):(\d+)\Z/) { print $&; }

Replies are listed 'Best First'.
Re: Need help in data processing
by davido (Cardinal) on Feb 26, 2011 at 09:06 UTC

    Do I understand correctly that the date isn't guaranteed to be of the same format all the time? If that changes, what is guaranteed to be the same always?

    I guess since your date format could change, you could use a module such as Date::Parse to tease the date out of any line containing the trigger anchor text. Perhaps you could capture the date like this:

    if( $textline =~ m/invoked by \w+ at (.+)$/ ) { $datetext = $1; }

    Then use Date::Parse's str2time() function to morph the date string into a unix time value:

    my $datetime = str2time( $datetext ) or die "Unrecognized date format. +\n";

    Dave

Re: Need help in data processing
by johngg (Canon) on Feb 26, 2011 at 14:13 UTC

    If I understand your question correctly, your problem lies with the fact that your log entry sometimes breaks over two lines between the date and the time. Simply reading line by line will not cater for this; you have to detect whether the entry has gone onto a second line and do another readline inside the loop if so.

    use strict; use warnings; open my $logFH, q{<}, \ <<EOD or die qq{open: <<HEREDOC: $!\n}; Script /opt/OV/bin/OpC/agtinstall/inst.sh invoked by root at 02/25/11 14:36:48 Other stuff here Script /opt/OV/bin/OpC/agtinstall/inst.sh invoked by root at 02/25/11 +14:36:48 more stuff and more Script /opt/OV/bin/OpC/agtinstall/inst.sh invoked by root at 02/25/11 14:36:48 more guff EOD my $rxDate = qr{(?x) ( (?:\d\d/){2}\d\d ) }; my $rxTime = qr{(?x) ( (?:\d\d:){2}\d\d ) }; while ( defined( my $line = <$logFH> ) ) { next unless $line =~ m{(?x) ^ Script \s \S+ \s invoked \s by \s root \s at \s $rxDate (?: \s $rxTime )? }; my $date = $1; my $time = defined $2 ? $2 : do { defined( $line = <$logFH> ) or die qq{Unexpected EOF\n}; $line =~ $rxTime ? $1 : q{No time found}; }; my $timestamp = join q{ }, $date, $time; print qq{Timestamp: $timestamp\n}; } close $logFH or die qq{close: <<HEREDOC: $!\n};

    The output.

    Timestamp: 02/25/11 14:36:48 Timestamp: 02/25/11 14:36:48 Timestamp: 02/25/11 14:36:48

    I hope I have understood correctly and that this is helpful.

    Cheers,

    JohnGG

Re: Need help in data processing
by Anonymous Monk on Feb 26, 2011 at 08:31 UTC

      Hi achak01,

      This might work for you

      #!/usr/bin/perl use strict; while(<DATA>){ if (m/^Script.+\sat\s(.+)\s(.+)$/){ my $date = $1; my $time = $2; print "$date\t$time\n"; } } __DATA__ Script /opt/OV/bin/OpC/agtinstall/inst.sh invoked by root at 02/25/11 +14:36:48 blah blah blah Script /opt/OV/bin/OpC/agtinstall/inst.sh invoked by root at 02/25/11 +14:36:48 blah blah blah

      Cheers

        Sorry! but this sol deosnt seem to work
Re: Need help in data processing
by umasuresh (Hermit) on Feb 26, 2011 at 13:32 UTC