From kragen@dnaco.net Wed Aug 26 08:11:38 1998
Date: Wed, 26 Aug 1998 08:11:36 -0400 (EDT)
From: Kragen <kragen@dnaco.net>
To: Jim Weirich <jweirich@one.net>
cc: clug-user@clug.org
Subject: Re: Web Page Help
In-Reply-To: <13795.59394.430486.378650@localhost.localdomain>
Message-ID: <Pine.SUN.3.96.980826075941.11646A-100000@picard.dnaco.net>
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
X-Keywords:
X-UID: 1435
Status: O
X-Status: 

On Wed, 26 Aug 1998, Jim Weirich wrote:
> >>>>> "Jim" == Jim Weirich <jweirich@one.net> writes:
>     Jim> What I need is a script that will split an ordinary mail file
>     Jim> into separate mail files by month.  

#!/usr/bin/perl -w
use strict;
# Append the messages in an mbox to mail files named by year and month.

# OK, here's a quick hack I whipped up.  It worked on test mailboxes
# created by Pine, qmail, and trn.  It's probably not the fastest way to
# do things (it opens and closes the output file for every message) (it
# took four seconds to split a 599-message, 1.1MB mailbox into seven
# month files on a 5x86-133 with Linux 2.0.30), and for God's sake, don't 
# run it in a world-writable
# directory on a multiuser machine.

# It puts everything in the current directory in files named things like
# '1998-Aug'.  This could be corrected to '1998-08' with little effort.

# It ought to tell you if it's failing, not just silently fail.

# Regexes are your friend.  (The regex used here could be improved.)

my $foundfrom = 0;
while (<>) {
        # samples:
        # From snoopy@wport.com Thu May 14 22:30:24 EDT 1998
        # From kragen@dnaco.net Tue Jan  6 09:23:04 1998
        if (/^From .*\s+([A-Za-z][A-Za-z][A-Za-z])\s+\d+\s+[0-9:]+\s+(?:\S+\s+)?((19|20)\d\d)\s*$/) {
                my ($month, $year) = ($1, $2);
                open OUTPUT, ">>$year-$month" or die "Can't open $year-$month: $!\n";
                $foundfrom = 1;
        } elsif (/^From /) {
                warn "Found spurious `From' line $.: $_";
        }

        if ($foundfrom) {
                print OUTPUT;
        } else {
                warn "Line $. precedes any valid From lines: $_";
        }
}


-- 
<kragen@pobox.com>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
We are forming cells within a global brain and we are excited that we might
start to think collectively.  What becomes of us still hangs crucially on
how we think individually.  -- Tim Berners-Lee, inventor of the Web


