in reply to Delete files by clock
The basic plan for what you want to do would probably be easiest if you use the "strftime()" function provided by the POSIX module. (This module is part of the "core" distribution for Perl -- every Perl installation has it.)
Compute the number of seconds in 48 hours, subtract that value from the current "seconds since the epoch" returned by the "time()" function, and use "strftime()" to convert the result into a date/time string of the form you need:
Once you have that, just open the current file for input, open a new file for output, then iterate reading a line at a time from the input, but don't write to the output until you see a line that contains a date equal to or greater than $date_string.$two_days_ago = time() - 2 * 24 * 60 * 60; $date_string = strftime("%m-%d-%Y %H:%M:%S", localtime($two_days_ago)) +;
(update: well, that "equal-to-or-greater-than" part is tricky, given that the string is "MO-DY-YEAR HR:MI:SC" -- you'll probably want to convert that to "YEAR-MO-DY HR:MI:SC" (s/(\d{2})-(\d{2})-(\d{4})/$3-$1-$2/;) so that you can do a simple "ge" or "le" string comparison. There are also a few different Date::* modules that you might find useful.)
All remaining input lines get written to the output, you close the files, and rename the new one to whatever the old one was called (that deletes the old one).
(updated to add a couple more links to documentation and a snippet to change the date format of the file data.)
... Sorry about all these updates... If the lines in the data file are not in chronological order (your two lines of sample data seem to be out of order), you'd need to check the date on each line from start to finish, to decide whether to write it to the output.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Delete files by clock
by erez_ez (Acolyte) on Aug 16, 2007 at 08:55 UTC |