我正在使用 sed 正则表达式从日志文件中提取一些信息,以便进一步使用它进行分析。我创建了以下命令,但它对我不起作用。
sed -e 's/\([0-9] [0-9]*.[0-9]*.[0-9]*\)[^@]* ([^@]*@[^[:spa ce:]]*).*F=<\([^ ]*\)>.*I=[\([0-9]\+\.[0-9]\+\.)].*$/\1\t\2/' logs
日志:
2017-02-13 10:31:55 1cd9Ev-003XiE-Sx ** [email protected] F=<[email protected]> R=dkim_lookuphost T=dkim_remote_smtp H=ah2.inboundmx.com [216.82.242.115] I=[147.75.228.64] X=TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256 CV=yes DN="/C=US/ST=California/L=Mountain View/O=Symantec Corporation/OU=Symantec.cloud CN=mail132.messagelabs.com": SMTP error from remote mail server after end of data: 553-Message filtered. Refer to the Troubleshooting page at\n553-http://www.symanteccloud.com/troubleshooting for more\n553 information. (#5.7.1)
2017-02-14 10:01:40 1cd9Ev-003XiE-Sx ** [email protected] F=<[email protected]> R=dkim_lookuphost T=dkim_remote_smtp H=ah2.inboundmx.com [216.82.242.115] I=[14.176.22.221] X=TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256 CV=yes DN="/C=US/ST=California/L=Mountain View/O=Symantec Corporation/OU=Symantec.cloud CN=mail132.messagelabs.com": 501 Connection rejected by policy. Refer to the Troubleshooting page at\n501-http://www.symanteccloud.com/troubleshooting for more\n501 information. (#5.7.1)
我想从上面的日志中提取以下字段:
Timestamp EmailTo: EmailFrom: IPAddress: ErrorCodes:
2017-02-13 10:31:55 [email protected] [email protected] 147.75.228.64 553
2017-02-14 10:01:40 [email protected] [email protected] 14.176.22.221 501
答案1
除了提取所需字段之外,另一个想法是删除额外的:
sed '
s/[^: ]*\s\*\*\s//
s/F=<//
s/>.*I=\[/ /
s/\].*more\\n/ /
s/\sinf.*//
' log.file
- 第一个命令删除
1cd9Ev-003XiE-Sx **
- 第二 -
F=<
- 第三 -
> R=dkim_lookuphost T=dkim_remote_smtp H=ah2.inboundmx.com [216.82.242.115] I=[
等等…