【发布时间】:2012-07-02 04:07:42
【问题描述】:
我想通过 bash 命令将我的收件箱拆分为单独的文件(每条消息一个文件),或者可以是 Java 中的简单程序。我该怎么做?
WBR,谢谢。
【问题讨论】:
标签: linux bash email unix mbox
我想通过 bash 命令将我的收件箱拆分为单独的文件(每条消息一个文件),或者可以是 Java 中的简单程序。我该怎么做?
WBR,谢谢。
【问题讨论】:
标签: linux bash email unix mbox
只需使用formail。 formail是一个程序,可以处理邮箱,对邮箱中的每条消息运行一些动作,分离消息等等。
更多信息:http://www.manpagez.com/man/1/formail/
如果您只想将邮箱拆分为单独的文件, 我会建议这样的解决方案:
$ cat $MAIL | formail -ds sh -c 'cat > msg.$FILENO'
来自男人:
FILENO
While splitting, formail assigns the message number currently
being output to this variable. By presetting FILENO, you can
change the initial message number being used and the width of the
zero-padded output. If FILENO is unset it will default to 000.
If FILENO is non-empty and does not contain a number, FILENO gen-
eration is disabled.
【讨论】:
formail 是我机器上 procmail 的一部分 (apt-get install procmail)。
formail 似乎很原始。它似乎只是根据以From: 开头的行来拆分文件。我的电子邮件在电子邮件正文的一行开头包含该短语,formail 将这些电子邮件一分为二,而不是将其视为一条消息。安德烈回答中的git 解决方案对我来说效果更好。
msg.000 文件。
如果你没有formail,你也可以使用这个Perl oneliner(复制自here,刚刚在我需要拆分的Thunderbird收件箱上测试过)
perl -pe 'open STDOUT, ">out".++$n if /^From /' < $IN > before_first
或者,使用 0 填充的数字:
perl -pe 'open STDOUT, sprintf(">m%05d.mbx", ++$n) if /^From /' < $IN > before-first
【讨论】: