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

Hi there!!!
How could I remove a date range in a text file like this one:
****2000-01-01****test1*****12345***** ...untill... ****2006-05-09****test1*****12345*****

It's a big text file, I need to get rid of everything from last year (2005). How can a delete based on a date range?
Any Help?

Replies are listed 'Best First'.
Re: Delete in a range of Dates
by blazar (Canon) on May 09, 2006 at 16:06 UTC

    Seems like a job you need to do only once. If so, and if the entries in the file are ordered by date as your example seems to imply, then why not... just use your favourite text editor?!?

    To do it with perl, just read the file sequentially parsing the entries and using a date managing module, like DateTime or Date::Calc to only print out those lines which lie in the wanted range. What have you tried thus far?

Re: Delete in a range of Dates
by dsheroh (Monsignor) on May 09, 2006 at 22:41 UTC
    Well... The easiest way is to just use grep, no need for perl:

    grep -v ^\*\*\*\*2005 filename

    This will print out all lines which don't start with ****2005.

    The perl equivalent would be the loop:

    while(<>) { print unless /^\*\*\*\*2005/; }
    but, again, not much point to the extra typing unless you need something extra that grep won't do for you.