in reply to Re: Extract the lines from file
in thread Extract the lines from file

#!/usr/bin/perl use Date::Manip; $ndays=$ARGV[0]; # Get the logs $date = ParseDate("$ndays days ago"); print UnixDate($date,"%m/%d/%Y") . "\n";
In the input file, the values are of 3 day and 2 day old date. I have to extract the time from Suppose date is : Thu Jul 8 2010 This is the inout file,
- [5/Jul/2010:00:59:59 +0000] - [5/Jul/2010:00:10:00 +0000] - [5/Jul/2010:06:10:00 +0000] - [5/Jul/2010:06:50:00 +0000] - [5/Jul/2010:07:10:00 +0000] - [5/Jul/2010:10:10:00 +0000] - [6/Jul/2010:06:10:00 +0000] - [6/Jul/2010:07:10:00 +0000] - [5/Jul/2010:08:10:00 +0000] - [5/Jul/2010:06:10:00 +0000] - [5/Jul/2010:09:10:00 +0000] - [5/Jul/2010:10:00:00 +0000] - [5/Jul/2010:10:15:00 +0000]
Extract the rows, where time is greater than 5/Jul/2010:06:00:00 and less than 6/Jul/2010:10:00:00 So the expected output is :
- [5/Jul/2010:06:10:00 +0000] - [5/Jul/2010:06:50:00 +0000] - [5/Jul/2010:07:10:00 +0000] - [5/Jul/2010:10:10:00 +0000] - [6/Jul/2010:06:10:00 +0000] - [6/Jul/2010:07:10:00 +0000] - [5/Jul/2010:08:10:00 +0000] - [5/Jul/2010:06:10:00 +0000] - [5/Jul/2010:09:10:00 +0000] - [5/Jul/2010:10:00:00 +0000]

Replies are listed 'Best First'.
Re^3: Extract the lines from file
by nudge (Acolyte) on Jul 09, 2010 at 07:08 UTC

    Still clear as mud anonymous monk. Since you have already found Date::Manip you should mostly there. Is this what you are after?

    #!/usr/bin/perl use Date::Manip; use Getopt::Long; my ($from_text, $till_text); GetOptions( from => \$from, till => \$till ); my $from_date = ParseDate($from_text); unless ($from_date) { die "Invalid From date\n"; } my $till_date = ParseDate($till_text); unless ($till_date) { die "Invalid till date\n"; } unless (Date_Cmp($from_date,$till_date) > 0) { die "Horribly\n"; } while(<>) { ... read the date ... if (Date_Cmp($date,$from_date) >= 0 and Date_Cmp($date,$till_date) < 0) { ... here we go ... } }