I have a number of e-mail messages (envelope, headers, body, sometimes including base64-encoded attachments) which are spread out among an arbitrary number of text files. In most cases, there are multiple messages in each text file.

I want to subset each large file so that each e-mail is saved to its own file. So, if the source file is "mails.txt" containing three messages, I want to create (e.g.) mails_000001.txt, mails_000002.txt, and mails_000003.txt.

I had kludged together a bash script that performed this operation thusly:

  1. using grep -n, note the line numbers for both blank lines (^$) and essential headers (e.g., "From:").
  2. Use those line number to determine the beginning of each message (that is, when a line beginning with "From" is discovered, I assume it is within a message, and then find the first blank line before that line)
  3. Subsetting each message using gawk.
However, I wish to create a perl-only solution for a number of reasons and I can't help but imagine that there is a slicker way to do this with perl. At this stage I'm mainly just looking for a general strategy for doing such an operation rather than full-fledged examples of code, but any assistance is welcome.

Thanks!


In reply to Subsetting text files containing e-mails by PeterCap

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.