我有一百多个日志文件,每个文件中都有0到20行,格式如下:
[2016-06-08 18:12:32] production.INFO:成功完成某件事。电子邮件:[email protected] [] {“ user”:“ anonymous”,“ url”:“ / something”,“ ip”:“ 77.46.189.212”,“ http_method”:“ POST”,“ server”:“ www.mysite.com”,“引荐来源网址”:“ www.mysite.com/something”,“ unique_id”:“ V1hD7lJ10JkAAAQ7MgsAAAAa”}
我如何使用grep从那些文件中提取所有电子邮件,但只提取出现在电子邮件前面的那些行Successfully done something. Email:
?
换句话说,应该忽略下一行中的电子邮件:
[2016-06-08 17:13:29] production.INFO:用户[email protected]注销... [] {“用户”:“ [email protected]”,“网址”:” / admin /注销”,“ ip”:“ 109.92.131.202”,“ http_method”:“ GET”,“服务器”:“ mysite.com”,“引荐来源网址”:“ www.mysite.com/admin/foo”,“ unique_id” :“ V1g2GVJ10JkAAAqy42gAAABH”}
从给定的示例中,我想提取[email protected]
并忽略[email protected]
。
您可以使用awk
两者来检查“成功完成某件事”。行发生,并且其中包含电子邮件:
awk '/Successfully done something. Email:/ && \ # match line
match($0, /Email: ([^ ]*) /, matches) { # match up to space
print matches[1] # print captured group
}' file
使用您给定的数据:
$ awk '/Successfully done something. Email:/ && match($0, /Email: ([^ ]*) /, matches) {print matches[1]}' file
[email protected]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句