使用 awk 解析日志条目
Posted
技术标签:
【中文标题】使用 awk 解析日志条目【英文标题】:Parsing log entries using awk 【发布时间】:2013-11-26 21:14:03 【问题描述】:我得到以下日志:
2013-10-24 18:35:49,728 ERROR [xx.xx.xx.xx.xx.xx] (xx.xx.xx.xx.xx) Manila Olongapo Leyte Tacloban has updated their subscriber details. But, the Regional Account Update interface call has failed for the following Local Registries: <br/>Visayas<br/>Data between LRA and the above Local Registries is out of synch as a result.
我希望结果输入采用以下格式。有什么更好的方法来做到这一点——也许使用awk
或sed
?请指教。
$Province$ has updated their subscriber details. However, the Customer Account Update interface call has failed for the following Land Registries:
$Region Name$
【问题讨论】:
欢迎来到 Stack Overflow。请尽快阅读About 页面。传入记录是否在 3 行上?您希望$Province$
和$Region Name$
出现在输出中吗?输入包含“区域帐户”但输出包含“客户帐户”是偶然还是故意的。 'Manila Olongapo Leyte Tacloban' 列表是否有所不同?
如果 $Region Name$ 或 $Province$ 不遵循与您示例中相同数量的字段,您想要的此日志解析器将失败。请使用更准确的要求编辑您的问题。
【参考方案1】:
只考虑你的一个例子,只是为了回答你的问题,这里是:
echo '2013-10-24 18:35:49,728 ERROR [xx.xx.xx.xx.xx.xx] (xx.xx.xx.xx.xx) Manila Olongapo Leyte Tacloban has updated their subscriber details. But, the Regional Account Update interface call has failed for the following Local Registries: <br/>Visayas<br/>Data between LRA and the above Local Registries is out of synch as a result.' | awk 'print $6,$7,$8,$9,$10,$11,$12,$13,$14,"However, the Customer",$18,$19,$20,$21,$22,$23,$24,$25,$26,"Land",$28,substr($29,6,7)'
【讨论】:
【参考方案2】:这在sed
中很容易做到:
sed -r '
s#(^.*\) |<br/>Data.*$)##g;
s/But/However/;
s/Regional/Customer/;
s/Local/Land/;
s# <br/>#\n#
' input.log
Manila Olongapo Leyte Tacloban has updated their subscriber details. However, the Customer Account Update interface call has failed for the following Land Registries:
Visayas
【讨论】:
OP 询问的是 awk,而不是 sed。以上是关于使用 awk 解析日志条目的主要内容,如果未能解决你的问题,请参考以下文章