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:
- using grep -n, note the line numbers for both blank lines (^$) and essential headers (e.g., "From:").
- 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)
- 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.