Håk*_*and 0 linux smtp email awk
我正在寻找一个简单的程序来将电子邮件从我的收件箱中提取到一个文件中以供进一步处理,例如使用awk
. 对于“提取”,我的意思是使用例如正则表达式来获取具有特定主题的电子邮件。
我的收件箱目前超过 1Gb,所以我使用tail
命令来获取最后一部分。以下是两条消息的示例:
From aaa.aaa@uni.no Tue Nov 26 17:27:33 2013
Return-path: <aaa.aaa@uni.no>
Envelope-to: fcihh@uib.no
Delivery-date: Tue, 26 Nov 2013 17:27:33 +0100
Received: from alfons.uib.no [2001:700:200:30::141]
by alfred.uib.no with esmtp (Exim 4.77)
id 1VlLU5-0003Gg-8R; Tue, 26 Nov 2013 17:27:33 +0100
Received: from thoralf.uib.no (smtp.uib.no) [2001:700:200:13:129:177:13:16]
by alfons.uib.no with esmtps (TLSv1:AES256-SHA:256) (Exim 4.77)
id 1VlLU5-0002Po-4p; Tue, 26 Nov 2013 17:27:33 +0100
Received: from cipr064089.klientdrift.uib.no [129.177.64.89]:35068
by smtp.uib.no with esmtpsa (Exim 4.69)
id 1VlLU5-0006dY-35; Tue, 26 Nov 2013 17:27:33 +0100
Message-ID: <5294CBFA.3030408@uni.no>
Date: Tue, 26 Nov 2013 17:27:38 +0100
From: bb b <bb.bb@uni.no>
Organization: Uni Research AS
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.1.1
MIME-Version: 1.0
To: b b <bbb.bbb@uni.no>
CC: Haakon Haegland <hakon.hagland@uni.no>
Subject: Re: ddd
References: <5294C625.9010403@uni.no>
In-Reply-To: <5294C625.9010403@uni.no>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit
X-checked-clean: by exiscan on alfons.uib.no
X-Scanner: 2db488888a1c80ee768332c03a147d00 http://tjinfo.uib.no/virus.html
X-UiB-SpamFlag: NO UIB: -18 hits, 8.0 required
X-UiB-SpamReport: spamassassin found;
-15 From is listed in 'whitelist_SA'
-3.0 Message received from Norway
X-sender_host_address: 2001:700:200:13:129:177:13:16
X-sender_host_name: thoralf.uib.no
X-UID: 28258
Status: RO
On 11/26/2013 05:02 PM, b b wrote:
> ggg
> hhh
qqq
From bbb.bbbk@uni.no Tue Nov 26 17:33:10 2013
Return-path: <bbb.bbb@uni.no>
Envelope-to: fcihh@uib.no
Delivery-date: Tue, 26 Nov 2013 17:33:10 +0100
Received: from alfons.uib.no [2001:700:200:30::141]
by alfred.uib.no with esmtp (Exim 4.77)
id 1VlLZW-0004jv-7u; Tue, 26 Nov 2013 17:33:10 +0100
Received: from thoralf.uib.no (smtp.uib.no) [2001:700:200:13:129:177:13:16]
by alfons.uib.no with esmtps (TLSv1:AES256-SHA:256) (Exim 4.77)
id 1VlLZW-0002wL-3T; Tue, 26 Nov 2013 17:33:10 +0100
Received: from cipr064186.klientdrift.uib.no [129.177.64.186]:57257
by smtp.uib.no with esmtpsa (Exim 4.69)
id 1VlLZW-0006iZ-29; Tue, 26 Nov 2013 17:33:10 +0100
Message-ID: <5294CD3D.9020407@uni.no>
Date: Tue, 26 Nov 2013 17:33:01 +0100
From: b b <bbb.bbb@uni.no>
Organization: Uni CIPR
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20131028 Thunderbird/17.0.10
MIME-Version: 1.0
To: a a <aaa.aaa@uni.no>
CC: Haakon Haegland <hakon.hagland@uni.no>
Subject: Re: ddd
References: <5294C625.9010403@uni.no> <5294CBFA.3030408@uni.no>
In-Reply-To: <5294CBFA.3030408@uni.no>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit
X-checked-clean: by exiscan on alfons.uib.no
X-Scanner: 467bf36c80a8700bd89776b495ccd761 http://tjinfo.uib.no/virus.html
X-UiB-SpamFlag: NO UIB: -17.9 hits, 8.0 required
X-UiB-SpamReport: spamassassin found;
-15 From is listed in 'whitelist_SA'
-3.0 Message received from Norway
0.0 BODY: ReplaceTags: Contact
X-sender_host_address: 2001:700:200:13:129:177:13:16
X-sender_host_name: thoralf.uib.no
X-UID: 28259
Status: RO
txtxt
Run Code Online (Sandbox Code Playgroud)
我猜这封电子邮件的格式是 SMTP http://en.wikipedia.org/wiki/SMTP_protocol?(我也看过MIME和RFC这两个词,不知道有没有关系)问题:
awk
提取具有给定主题字段的消息?消息的格式是RFC 2282,即 Internet 消息格式。该文件就像你格式的声音正在描述MBOX格式,这是RFC 4155。
根据 RFC 4155,mbox 数据库中的新消息以From
关键字开头,一个空格然后是报告的发件人的地址,一个空格和收到消息时的 UTC 日期。
此后标题字段由换行符分隔并以字母开头。如果新行的第一个字符是空格(我相信通常是制表符),那么它是前一个标题行值的延续。标题键与标题值由:
(冒号空格)分隔。
标题和正文由一个空行分隔。每条消息也由空格分隔。
至于用 提取主题awk
,首先提取标题,然后通过它传递:
awk '/^Subject: .*/ {print substr($0,10)}'
Run Code Online (Sandbox Code Playgroud)
这将打印从位置 10 开始的行。