Henry Wong wrote: > > > Henry Wong wrote: > > > > > > I am wondering if you guys can help me out here: > > > I have a log file containing information like this: > > > > > > THU DEC 6 14:55:00 2001 111 222 333 444 > > > FRI DEC 7 01:00:00 2001 555 666 777 888 > > > SAT DEC 8 13:00:00 2001 xxx xxx xxx xxx > > > SUN DEC 9 04:00:00 2001 xxx xxx xxx xxx > > > MON DEC 10 12:00:00 2001 xxx xxx xxx xxx > > > TUE DEC 11 09:00:00 2001 xxx xxx xxx xxx > > > > > > Having the above log file with the first column containing the usual TIME, > > > following by 4 columns of numerical data, I am thinking of having this > > > program asking the user to input his/her desired starting date & ending > > > date, and thus thereby extracting all the relevant 4 column datas > > > corresponding to the above dates. > > > > > > What i'm trying to say is, lets say the user inputs START DATE to be THU > > > DEC 6, and END DATE to be MON DEC 10. So my code will then extract all > > > relevant data corresponding to THU DEC 6 (which is 111, 222, 333, 444) up > > > till MON DEC 10. That means the code would've extracted all 5 rows of data > > > with their corresponding 4 xxx columns (from start date to end date), and > > > then putting it into a file. > > Ok, i've decided to skip using the Date::Manip coz its giving me lots of > problems. Is there a way to compare dates (e.g. Thu Jun 20 12:00:00 2002) > and sort them in order? What i've always wanted is to sort them, and > subsequently using User's input of Start & End date to capture relevant logs > as explained below. > > Assuming that $noforlines is the no of lines the log file has, and that > $convertedList[] contains the dates, while $user_start & $user_end > containing the user's desired start & end date, how do i compare dates? The > below coding doesn't seem to work right. > > for ($s=0;$s<$noforlines;$s++) { > if (($convertedList[$sian]>= $user_start)&&($convertedList[$sian]<= > $user_end)) { > open (USER, ">>${filename}_REQUEST.TXT"); > print USER "$convertedList[$sian]\t$inputList[$sian]\n"; > }
You can use the Time::Local module to convert the log file dates to epoch time (number of seconds since the epoch date.) use Time::Local; my $wdayre = qr/mon|tue|wed|thu|fri|sat|sun/i; my %months = qw/jan 0 feb 1 mar 2 apr 3 may 4 jun 5 jul 6 aug 7 sep 8 oct 9 nov 10 dec 11/; my $monre = qr/@{[ join '|', keys %months ]}/i; while ( <LOG> ) { next unless /$wdayre\s+($monre)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)/ $epoch_time = timelocal( $5, $4, $3, $2, $months{$1}, $6 - 1900 ); if ( $epoch_time > $start_time and $epoch_time < $end_time ) { # do stuff here } } John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]