Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing Old Transactions from a File 2

Status
Not open for further replies.

tbohon

Programmer
Apr 20, 2000
293
US
I have a file containing records which have a date in the form MM/DD/YYYY at the beginning of each record. Since this file grows by the appending of current transactions every day, it's going to be huge in short order. I'd like to go in and prune anything older than 30 days based on this date value as part of my daily processing script. I've looked at 'man date' without success and done a search here and in my reference books (all 3 of them) ... again, no luck.

I had thought I could convert the 'pivot' date - 30 days ago - to seconds (I have a .ksh script to do that) and then search the file, converting each record's date to seconds (that's problem #1, I don't know how to do that in Perl) and compare the two. All records with a converted value less than the pivot date value would not be written to the new file.

Any thoughts/comments/suggestions/one-way trips to Kauai would be most appreciated! :)

Tnx in advance.

Tom

"My mind is like a steel whatchamacallit ...
 
To get the pivot date of thirty days ago:


Code:
use POSIX qw(strftime)

my $pivotdate = strftime("%Y/%m/%d", localtime(time - 30 * 24 * 60 * 60));

Then simply to a translation of the americanized dates into the above format and to a string compare.

Code:
my $date = '11/29/2006';

# Translate MM/DD/YYYY to YYYY/MM/DD
$date =~ s{(\d+)/(\d+)/(\d+)}{$3/$2/$1};

if ($date lt $pivotdate) {
   print "$date is less than $pivotdate\n";
} else {
   print "$date is greater than $pivotdate\n";
}
 
It seems to me that if you wanted the last thirty days of records there is no need to specifically check the value of the date. Simple count the number of thimes the date changes. When you reach a count of 31 you know you have the last thirty days and can stop searching.

If the file is appended to, the records should be on the end of the file, so read the file backwards using Tie::File or File::Readbackwards and count the number of date changes until you reach the desired amount. Make a new file or overwrite the existing file with the last thirty days of records you pulled from the original file.

- Kevin, perl coder unexceptional!
 
Thanks much, gentlemen ... much more elegant solutions than I've come up with, will give them both a shot when I get back to work in the morning.

Appreciate it!

Tom

"My mind is like a steel whatchamacallit ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top