geek.shrek has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I would like to filter my log file within time range For example my log.txt as below, and I want to get data only if the 2nd coloumn is from 05:00:00 until 24:00:00
"02-feb-2010","04:59:12;09 ","00:00:06;27 " ,"ABC1234 " +,"ABC1234","ABC1234","610A/03","00:08:19;28 ","00:08:26;24.","02-feb- +2010","04:59:19;06 " "02-feb-2010","04:59:19;06 ","00:00:15;00 " ,"creeid00402 + ","OY00402W","OY00402W","610D/00","04:00:00;00 ","04:00:14;29.","02 +-feb-2010","04:59:34;06 " "02-feb-2010","04:59:34;06 ","00:29:01;25 " ,"ihw-14ubc-q + ","3316291-","3316291-","610E/00","03:00:00;00 ","03:29:01;24.","02 +-feb-2010","05:28:35;29 " "02-feb-2010","05:28:35;29 ","00:00:15;00 " ,"creeid00402 + ","OY00402W","OY00402W","610D/00","04:00:00;00 ","04:00:14;29.","02 +-feb-2010","05:28:50;29 " "02-feb-2010","05:28:50;29 ","00:00:30;00 " ,"scifim10078 + ","SF10078W","SF10078W","610F/00","02:00:00;00 ","02:00:29;29.","02 +-feb-2010","05:29:21;01 " "02-feb-2010","05:29:21;01 ","00:00:15;00 " ,"creeid00402 + ","OY00402W","OY00402W","610D/00","04:00:00;00 ","04:00:14;29.","02 +-feb-2010","05:29:36;01 "
I tried to use grep, but I have no luck and I don't have any idea how to range the time.
grep -e "(.*),("05:.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*)"
I also tried
my @lines = (); push @lines, $_ if /(.*),("05:.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*), +(.*),(.*)/; foreach my $line (@lines) { print "LINE : " . $lines[0] ." \n"; }
Can someone help me please. Thanks,

Replies are listed 'Best First'.
Re: Filter log file within time range
by toolic (Bishop) on Feb 03, 2010 at 23:47 UTC
    Quick and dirty:
    use strict; use warnings; while (<DATA>) { my $time = (split /,/)[1]; my ($hr) = $time =~ /^"(\d\d)/; print if $hr>=5 and $hr<=23; } __DATA__ "02-feb-2010","04:59:19;06 ","00:00:15;00 " ,"creeid00402 + ","OY00402W","OY00402W","610D/00","04:00:00;00 ","04:00:14;29.","02 +-feb-2010","04:59:34;06 " "02-feb-2010","04:59:34;06 ","00:29:01;25 " ,"ihw-14ubc-q + ","3316291-","3316291-","610E/00","03:00:00;00 ","03:29:01;24.","02 +-feb-2010","05:28:35;29 " "02-feb-2010","05:28:35;29 ","00:00:15;00 " ,"creeid00402 + ","OY00402W","OY00402W","610D/00","04:00:00;00 ","04:00:14;29.","02 +-feb-2010","05:28:50;29 " "02-feb-2010","05:28:50;29 ","00:00:30;00 " ,"scifim10078 + ","SF10078W","SF10078W","610F/00","02:00:00;00 ","02:00:29;29.","02 +-feb-2010","05:29:21;01 "
    If any of your fields could have commas, you'd be better off with a CSV parser, such as Text::CSV_XS.
Re: Filter log file within time range
by apl (Monsignor) on Feb 04, 2010 at 13:31 UTC
    What steps would you perform if you were doing this by hand? Don't worry about the Perl; explain the solution to yourself, and you're more than halfway to writing the program.