Have you written any perl scripts yet? Have you tried to write any code for this task (even pseudo-code)?

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:

$two_days_ago = time() - 2 * 24 * 60 * 60; $date_string = strftime("%m-%d-%Y %H:%M:%S", localtime($two_days_ago)) +;
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.

(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.


In reply to Re: Delete files by clock by graff
in thread Delete files by clock by erez_ez

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.