in reply to Extracting Line from Text file over 45 days old

Howdy :)

This is quite simple. Because your data is regular, you can use split to extract the date/time stamp from each record. Then you can use something like Date::Parse to convert the date/time stamp into a unix timestamp. After that, it's just a bit of simple arithmetic.

Here is some example code to demonstrate:

#!/usr/bin/perl -l use strict; use warnings; use Date::Parse; my $cutoff_date = time - (45 * 86400); while (my $line = <DATA>) { chomp($line); my $date = (split /\|/, $line)[15]; my $unixdate = str2time($date) or next; print "I would ", $unixdate < $cutoff_date ? "archive" : "not arch +ive", " $date"; } __DATA__ type|address|city|state|size|rent|term|company|contact|phone|email|web +site|ID|REMOTE_ADDR|HTTP_USER_AGENT|DATE|SORTORDER MFG|10 Oakmead Pkwy|San Jose|CA |10,000|$1.25|Net|Cushman Wakefield|To +m Cushman|408 555-8777|tom@cushman.com|http://www.google.com|1000000| +76.102.98.35|Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET +CLR 2.0.50727)|16:01:08 2007-01-10|1003 LAND|10 Oak|San Jose|CA|14,000|$1.25|Net|Ritchie Commercial|Don Ritchi +e|408 555-8777|tom@cushman.com|http://www.yahoo.com|1000001|76.102.98 +.35|Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.5 +0727)|16:28:03 2008-01-10|1002
Which prints:
I would archive 16:01:08 2007-01-10 I would not archive 16:28:03 2008-01-10

Hope this helps,
Darren :)

Replies are listed 'Best First'.
Re^2: Extracting Line from Text file over 45 days old
by chris654 (Initiate) on Jan 12, 2008 at 06:12 UTC
    I replaced <data> with database.txt but when I ran it got the following error

    Can't locate Date/Parse.pm in @INC (@INC contains: /usr/lib/perl5/5.8.0/i386-linux /usr/lib/perl5/5.8.0 /usr/lib/perl5/site_perl/5.8.0/i386-linux /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl .) at ./over45.sh line 4. BEGIN failed--compilation aborted at ./over45.sh line 4.

    Is Date::Parse not supported on my virtual server or is something else wrong.

    Thanks,
    Chris

      That error most likely means that the Date::Parse module is not installed. Installing it should be as simple as:
      perl -MCPAN -e "install Date::Parse"

      Cheers,
      Darren :)

        Or, if you are running your program on a server run by a hosting company which it sounds like you are, ask your hosting company to install the module for you if you don't know how.