I just wrote a script that does what I want - i.e., trimming a "catch-all" mailbox to just the last N days worth of emails. However, I'm a little uncomfortable with the basic design of the program - it just seems like there should be a smarter way to do this, somehow. Any suggestions for improvement would be appreciated.

(Incidentally, the reason that I'm using 'date --date=""' is that a) it's very smart about figuring out the variety of dates one runs into in email headers, and b) the various modules I've tried are either not smart enough or slower than 'date'.)

#!/usr/bin/perl use warnings; use strict; die "Usage: ", $0 =~ /([^\/]+)$/, " <mbox> [days]\n" unless -f $ARGV[0] && @ARGV >= 1 && @ARGV <= 2; my $days = $ARGV[1] || 3; pop if $ARGV[1]; my $cutoff = time - $days * 60 * 60 * 24; my ($found, $content); while (<>){ print && next if $found; if (/^From /../^$/){ $content .= $_; if (/^Date: ([-+:,)(\w ]+)$/){ my $mdate = `date --date="$1" "+%s"`; if ($mdate >= $cutoff){ $found++; print $content; } } } else { $content = ""; } }

Update: Thanks to the advice from [thargas], the date is now validated. Despite the jocular tone in my response to his post, it was a serious issue - a 'Date: $(rm -rf ~/*)' would indeed have done some serious damage if it somehow got through the mail filters.

-- 
I hate storms, but calms undermine my spirits.
 -- Bernard Moitessier, "The Long Way"

In reply to Trimming a mailbox by oko1

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.