Hi Monks!
I need to compare dates on every text file found in a directory, and if the file contents are older than one moth from today's date, I need to archive from that point, how could I compare this:
our $localtime = localtime;
# This $localtime is going to give me: "Fri Jan 27 12:11:15 2006" as an example.
# I have to match against multiple lines in these text files, a directory could have multiple text files like that. error1.txt, error2.txt error-test.txt. And each line in each file starts like:
[Dec 23 15:40:06 2005] derror.txt : Test application module :: Error:: +file_log ::: Bad file descriptor
Now, if [Dec 23 15:40:06 2005] is one month older from today's date I have to delete that line from the file(s) and archive it.
I have got to a point where I can go through the directories and to the files, open all file contents, but now comes my problem, how to do what I explained before? Find in the files lines that are a month older, clear the lines with old stuff and archive it.
I have some code to show how far I could go.
Any help will be great!
## OPEN AND READ THE DIRECTORY #=comment opendir (DIR, "$dir"); my @FILES = grep(/.txt/,readdir(DIR)); closedir (DIR); foreach my $FILES (@FILES) { open FILE, "$dir/$FILES" or warn "Can't open $FILES: $!<br>\n", n +ext; while(<FILE>){ my $check = $_; + if($check=~/(^\[)(\w+)(\s)(\w+)(\s)(\d+)(.*?)$/isg){ print "**** $2** $4**$6**$7**<br>";} } close FILE; } }


Thanks a lot for the help!

jdporter added more code tags


In reply to Comparing Dates by Anonymous Monk

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.