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

Hello Monks!
I am a little stuck, maybe you could help ...
I have to parse a logfile on a windows machine which contains (at the last line)
$string as mentioned below, i want to match against this last line and $string should contain the actual date ...

my $string='19.09.2011 ======== Ende Update'; my $date = `Date /T`; my $regex ="$date Ende ";
To simplify i only want to check for $date and "Ende" on this line ...
My problem is there are whitespaces or other characters between $date and Ende
So how to change the regex to match? Any help?

Thanks
MH

Replies are listed 'Best First'.
Re: Parse Log File for Date and string?
by Caio (Acolyte) on Sep 19, 2011 at 10:27 UTC
    IF i did get this straight, you want to extract the date and "Ende" from the last line.. so I'd extract it with

    /(\d{2}\.\d{2}\.\d{4})\s*=*\s*(Ende)/

    which goes for 2 digits dot 2 digits dot 4 digits any number of spaces followed by any number of = followed by any number os spaces again followed by Ende

    In the end you can capture the date and "ende" separately with $1 and $2, while discarding the:
    " ============ " in between.

    Hope it helps ;)
      Thanks for all the help now it works

      use strict; use warnings; my $returncode; $returncode = 0; open (my $IN,"<E:/scripts/eva/allupdates.log") || die "can not open da +tei: $!"; #regex für Datumsanalyse matcht auf Datum z.B. 30.11.2010 my $redate='((?:(?:[0-2]?\\d{1})|(?:[3][0,1]{1}))[-:\\/.](?:[0]?[1-9]| +[1][012])[-:\\/.](?:(?:[1]{1}\\d{1}\\d{1}\\d{1})|(?:[2]{1}\\d{3})))(? +![\\d])'; # DDMMYYYY 1 #Datumsbestimmung tatsächliches Datum: (my $sec, my $min, my $hour, my $mday,my $mon, my $year, my $wday, my +$ydat, my $isdst)=localtime(); my $jahr=$year; my $monat=$mon+1; my $tag=$mday; $jahr=$year; $jahr=$year +1900; if (length($monat) == 1) { $monat="0$monat"; } if(length($tag) == 1) { $tag="0$tag"; } if(length($hour) == 1) { $hour="0$hour"; } if(length($min) == 1) { $min="0$min"; } if(length($sec) == 1) { $sec="0$sec"; } my $Xdatum=$tag.".".$monat.".".$jahr; my $Xzeit=$hour.":".$min.":".$sec; #$Xdatum="$jahr$monat$tag"; #$Xzeit="$hour$min$sec"; #print $Xdatum." ".$Xzeit; #Datum im Logfile evawp.log my $datelogfile; #Vergleich Datum in Logdatei mit Systemdatum, wenn dies gleich ist, ok +, $redate matcht auf Datumsstring, der 1. String ist das Datum ... while(<$IN>){ if( /($redate)(\s+).*?(Ende)/i){ print " ,1. Variable $1\n"; $datelogfile = $1; print " Datelogfile $datelogfile\n"; print " Var Xdatum $Xdatum\n"; if ( $datelogfile eq $Xdatum) { print " Variable datelogfile $datelogfile\n"; $returncode++; } print; print "$returncode\n"; } } #wenn error nicht gefunden wurde dann auch die Datei nicht öffnen zum +schreiben ... #für jeden Fund wird returncode um 1 erhöht ... if ( $returncode == 1 ){ print "Update TS ok!\n"; } #Wenn Returncode 0 ist der String nicht gefunden, dann Fehler ... if ( $returncode <= 0 ){ print "Update TS FEHLER!\n"; } #print "$returncode\n"; close $IN; #print "$datelogfile \n"; system("pause");

      Thanks
      MH

        sprintf is your friend:

        my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $ydat, $isdst ) = l +ocaltime(); my $jahr = $year + 1900; my $monat = sprintf "%02d", $mon + 1; my $tag = sprintf "%02d", $mday; $hour = sprintf "%02d", $hour; $min = sprintf "%02d", $min; $sec = sprintf "%02d", $sec;
Re: Parse Log File for Date and string?
by RichardK (Parson) on Sep 19, 2011 at 10:36 UTC
    Perhaps reading the perlretut regex tutorial might help ?
Re: Parse Log File for Date and string?
by norbert.csongradi (Beadle) on Sep 19, 2011 at 09:58 UTC

    To tell the truth, I do not clearly see it as a regexp...

    Maybe you are looking for something like this (it consumes any other characters in between, trying to match as little as possible, so not consuming your Ende):

    my $regex = "$date.*?Ende";