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

Hello, I am trying to write a perl code where I grep a flat file for data records which is last 45 days old.

Each of the record in the flat file looks like this

"Fri Dec 3 23:07:21 EST 2004 : Depth is : 234". "Sat Dec 4 00:07:29 EST 2004 : Depth is : 123" "Sat Dec 5 03:07:32 EST 2004 : Depth is : 144"
Thank you

2005-03-12 Janitored by Arunbear - added code tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: grep for last 45 days
by Fletch (Bishop) on Mar 12, 2005 at 21:35 UTC

    Well, looking at the code you've provided showing what you've tried so far . . .

    Oh, you didn't. Never mind.

    Read the line. Extract the timestamp. Parse the timestamp (see Date::Manip, Date::Calc). Compare with now. Print if it's within your window.

    Update:

    require 'time' now=Time.now ARGF.each do |line| next unless line =~ /(.*?) : Depth/ d_days = ( now - Time.parse( $1 ) ) / 86400 puts line if d_days < 45 end
      The code worked for me. The only trick, it would appear, is to first run ln -s /usr/bin/ruby /usr/bin/perl.

      I was initially inclined to downvote the node for the horrible blasphemy of posting non-Perl code in the Monastary. Then I saw in my mind's eye the OP pasting the code into a file and trying to run it... what the... missing semicolon? I thought these guys knew their stuff!.

      So ++Fletch. I have Programming Ruby sitting on my bedside table, but have only browsed it so far. Ruby appears to be popular in Asia, and I really enjoyed some dabbling I've done in Smalltalk, so it might be right up my alley.

        Blasphemy for not using perl? Remember: TMTOWTDI, and some of the W's don't even involve perl . . . :)

        But yes that's kind of the point of using Ruby, especially if it's got a whiff of that homework smell about it (as this did) or if they've not shown any attempt at making an effort (again, as this did). Much of Ruby's close enough to Perl conceptually that you can (almost) see the equivalent Perl if you squint hard enough. Not to mention it gives an excellent chance for me to practice my Ruby.

      graff/fletch/tedpride/, Thank you very much The CODE WORKS!!!! Regards Greatfull
      Hi Fletch/TedPride, Thank you for the update ..BUT I am not sure if this gets all the records for the last 45 days. I did try the code you pasted...but its not showing all the records for the last 45 days Is there any other way of doing this in either perl or unix.
Re: grep for last 45 days
by graff (Chancellor) on Mar 13, 2005 at 03:01 UTC
    Um, about your sample data... I hope that isn't really what you have to cope with, because Dec. 5 2004 was a Sunday, not a Saturday. (And I hope you'll have at least some actual data that falls within the last 45 days, else what would be the point of a script that applies that threshold?)

    As for your problem, if you look at the docs for the first module suggested in the first reply, and just play with Date::Manip in the simplest possible experiment, I think you'd get what you want. For example (replacing your bad sample data with some relevant data):

    #!/usr/bin/perl use strict; use Date::Manip; my $limit = DateCalc( 'now', '- 45 days' ); while (<DATA>) { my $datestr = (split / : /)[0]; # note spaces around ":" print if ( ParseDate( $datestr ) > $limit ); } __DATA__ Fri Dec 3 23:07:21 EST 2004 : Depth is : 234 Sat Dec 4 00:07:29 EST 2004 : Depth is : 123 Sat Feb 26 14:13:12 EST 2005 : Depth is : 4567
Re: grep for last 45 days
by TedPride (Priest) on Mar 12, 2005 at 22:22 UTC
    Something like:
    use strict; use warnings; my $time = time(); my $days = 45; my ($day, $mon, $mday, $hour, $min, $sec, $zone, $year, $depth, $flag) +; while (read(DATA, $_, 45)) { if (!$flag) { ($day, $mon, $mday, $hour, $min, $sec, $zone, $year, $depth) = unpack('A3xA3xA2xA2xA2xA2xA3xA4x14A3', $_); # Use module to convert to timestamp $mytime if ($mytime > $time - 86400 * $days) { print $depth; $flag = 1; } } else { ($depth) = unpack('x42A3', $_); print $depth; } } __DATA__ Fri Dec 3 23:07:21 EST 2004 : Depth is : 234Sat Dec 4 00:07:29 EST 2 +004 : Depth is : 123Sat Dec 5 03:07:32 EST 2004 : Depth is : 144

    Problem is, I don't know a good module for creating a timestamp out of the date information. Maybe someone else can insert that part for me?

Re: grep for last 45 days
by talexb (Chancellor) on Mar 13, 2005 at 04:54 UTC

    I would recommend you dump the data into a database, then get the database to extract the data you want.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds