假设我有一个来自smtp日志的文本文件(如下所示),grep在这个日志文件中提取电子邮件的正则表达式是什么?
Jun 4 17:19:12 username postfix/pickup[22643]: C92866601: uid=33 from=<www-data>
Jun 4 17:19:12 username postfix/cleanup[23012]: C92866601: message-id=<20120604151912.C92866601@username.website.com>
Jun 4 17:19:12 username postfix/qmgr[2591]: C92866601: from=<www-data@email.com>, size=1314, nrcpt=2 (queue active)
Jun 4 17:19:13 username postfix/smtp[23014]: C92866601: to=<foobar@gmail.com>, relay=smtp.domain.com[134.96.7.25]:25, delay=0.5, delays=0.132, delays=11/0.01/0.09/0.21, dsn=2.0.0, status=sent (250 2.0.0 q53M4Dg6001057 Message accepted for delivery)
Jun 4 17:19:13 username postfix/smtp[23014]: C92866601: to=<mail-user@domain.com>, relay=smtp.domain.com[134.96.7.25]:25, delay=0.5, delays=0.132, delays=11/0.01/0.09/0.21, dsn=2.0.0, status=sent (250 2.0.0 q53M4Dg6001057 Message accepted for delivery)
Jun 4 17:19:13 username postfix/qmgr[2591]: C92866601: removed
Jun 4 17:19:13 username postfix/pickup[22643]: E146B6601: uid=33 from=<www-data>
Jun 4 17:19:13 username postfix/cleanup[23012]: E146B6601: message-id=<20120604151913.E146B6601@username.website.com>
Jun 4 17:19:13 username postfix/qmgr[2591]: E146B6601: from=<www-data@email.com>, size=1327, nrcpt=2 (queue active)
Jun 4 17:19:14 username postfix/smtp[23014]: E146B6601: to=<mail-user@domain.com>, relay=smtp.domain.com[134.96.7.31]:25, delay=0.43, delays=0.132, delays=11/0.01/0.09/0.21, dsn=2.0.0, status=sent (250 2.0.0 q53M4Dg6001057 Message accepted for delivery)
Jun 4 17:19:14 username postfix/smtp[23014]: E146B6601: to=<barbar@gmail.com>, relay=smtp.domain.com[134.96.7.31]:25, delay=0.43, delays=0.132, delays=11/0.01/0.09/0.21, dsn=2.0.0, status=sent (250 2.0.0 q53M4Dg6001057 Message accepted for delivery)
Jun 4 17:19:14 username postfix/qmgr[2591]: E146B6601: removed
Jun 4 17:19:14 username postfix/pickup[22643]: EF1606601: uid=33 from=<www-data>
Jun 4 17:19:14 username postfix/cleanup[23012]: EF1606601: message-id=<20120604151914.EF1606601@username.website.com>
Jun 4 17:19:15 username postfix/qmgr[2591]: EF1606601: from=<www-data@email.com>, size=1329, nrcpt=2 (queue active)
Jun 4 17:19:15 username postfix/smtp[23014]: EF1606601: to=<mail-user@domain.com>, relay=smtp.domain.com[134.96.7.31]:25, delay=0.42, delays=0.132, delays=11/0.01/0.09/0.21, dsn=2.0.0, status=sent (250 2.0.0 q53M4Dg6001057 Message accepted for delivery)
Jun 4 17:19:15 username postfix/smtp[23014]: EF1606601: to=<foofoo@gmail.com>, relay=smtp.domain.com[134.96.7.31]:25, delay=0.42, delays=0.132, delays=11/0.01/0.09/0.21, dsn=2.0.0, status=sent (250 2.0.0 q53M4Dg6001057 Message accepted for delivery)
Jun 4 17:19:15 username postfix/qmgr[2591]: EF1606601: removed
答案 0 :(得分:5)
grep -o '[-a-zA-Z0-9.]*@.[^>]*' filename
我将字母,数字,连字符和点视为有效的电子邮件名称。
如果您不确定日志文件中使用的电子邮件名称约定,请尝试以下操作:
grep -o '<[^@]*@.[^>]*' filename
上述命令会在电子邮件前面添加<
。