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

Hi, can anyone help with a code snippet to read the sample log file below and generate the output to a log file as shown below based on date. It has to always grab the previous day data. For example if I run the script today it has to output yesterday's data.

sample log file

Test_log sample Data: ##################### SYNC STARTED: Mon Sep 26 03:10:05 ##################### receiving file list ... done .d..t.... HEFFILE0004/02720220R/METADATA/ .d..t.... HEFFILE0004/60530610R/METADATA/ .d..t.... HEFFILE0004/60530700R/METADATA/ .d..t.... HEFFILE0004/60531070R/METADATA/ .d..t.... HEFFILE0004/60910750R/METADATA/ ##################### SYNC FINISHED: Mon Sep 26 05:15:39 EDT 2011 ### +################## ##################### SYNC STARTED: Tues Sep 27 03:10:05 ##################### receiving file list ... done .d..t.... HEFFILE0004/02720220R/IMAGES/ .d..t.... HEFFILE0004/60530610R/METADATA/ ##################### SYNC FINISHED: Tues Sep 27 08:15:39 EDT 2011 ## +###################
Required Output HEFFILE0004/02720220R HEFFILE0004/60530610R HEFFILE0004/60530700R HEFFILE0004/60531070R
Thanks,

Replies are listed 'Best First'.
Re: Reading a log file
by blue_cowdawg (Monsignor) on Sep 30, 2011 at 17:57 UTC

    You're asking for more than a snippet there...

    my $date = { set the date pattern you are looking for... } my $in_date = 0; while (my $line=<LOG>){ chomp $line; if ( ($in_date == 0 ) && ($line =~ m@SYNC@) && ($line =~ m@STARTED +@){ $line =~ s/\#//g; $line =~ s/^\s+//; $line =~ s/\s+$//; my @f=split(/\s+/,$line); shift @f; shift @f; my $rdate = join(" ",@f); if ( $date eq $rdate ) { $in_date=1; } } elsif ( $in_date && ($line =~ m@SYNC@ ) && ( $line =~ m@FINISHE +D@ ) ) { $in_date = 0; } elsif ( $line =~ m@HEFFILE@ ) { printf "%s\n",$line; } }

    That's the basic gyst of it. You may want to tweak it and I won't guarantee there aren't bugs...


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Reading a log file
by choroba (Cardinal) on Sep 30, 2011 at 18:05 UTC
    You can do this in bash :)
    date=$(date --date=yesterday +'%b %d') sed -n "/ $date /,/ $date /p" \ | grep -Ev '^ #|SYNC |receiving file' \ | cut -f2- -d' ' \ | rev | cut -f3- -d/ | rev