Re: script for an mbox

[email protected] (Gunnar Hjalmarsson)
Newsgroups perl.beginners
Message-ID <[email protected]>
Tony Heal wrote:
> I need to remove all messages older than X from a gigabyte size mbox. Anyone 
> got a script for this?

Here is a quick-and-dirty script to start with:

     use Date::Parse;

     sub isOld {
         my $date = shift;
         my $time = str2time($date);
         my $cmptime = '1210377600';
         $time < $cmptime ? 1 : 0;
     }

     my $date_re = qr/^Date:\s*(.+)/m;

     my $msg = <>;

     while (<>) {
         if ( /^From / ) {
             my ($date) = $msg =~ $date_re;
             print $msg unless isOld($date);
             $msg = $_;
         } else {
             $msg .= $_;
         }
     }

     my ($date) = $msg =~ $date_re;
     print $msg unless isOld($date);

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.