If you're having problems because of a bare 'From ' at the beginning of a line in your mail file, this is due to a bug in the program that's writing this to your mail file. I would look at solving the problem at the source instead of periodically running a fix like this to patch up the file. (Update: As coyote mentions, apparently it is standard behavior for stock Solaris sendmail to not quote bare Froms in messages, so if you're on this platform you'll need to modify this sendmail behavior or adapt your mailbox parser to take the 'Content-Length' header of your messages, assuming they're there, into account.)
Unfortunately, writing a Perl script to parse a Unix mailbox file to fix a problem like this is non-trivial. I mean you can probably get a regexp that'll match 98% of the messages, but generally parsing a Unix mailbox file relies on the fact that a bare 'From ' at the beginning of a line separates one message from the next.
If you can't identify/fix whatever software is causing this problem and still want to proceed along this line, something like this might work:
perl -pi.bak -e 's/^(?=From )(?![^\@]+\@)/>/' mailbox.file
This changes leading 'From's that do not have an @ anywhere on the line to >From, which is usually how this is handled by mail delivery agents. | [reply] [d/l] |
| [reply] |
| [reply] |
It's not Perl, but you might consider using formail for this. A snippet from the formail manpage:
NAME
formail - mail (re)formatter
[...]
DESCRIPTION
formail is a filter that can be used to force mail into
mailbox format, perform `From ' escaping, generate auto-
replying headers, do simple header munging/extracting or
split up a mailbox/digest/articles file.
| [reply] [d/l] |
| [reply] |