grep和打印反向引用 [英] Grep and print back reference

查看:231
本文介绍了grep和打印反向引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的iptable日志:

I have this iptable log:

Feb 25 10:32:48 XXX: [414645.555838] FW: DEN TCP IN=eth0 OUT= MAC=XYZ SRC=1.1.1.1 DST=2.2.2.2 LEN=40 TOS=0x00 PREC=0x00 TTL=57 ID=0 DF PROTO=TCP SPT=80 DPT=51814 WINDOW=0 RES=0x00 RST URGP=0

我要到grep 1.1.1.1和80(SRC和SPT场)。
我这样做:

I want to grep 1.1.1.1 and 80 (SRC and SPT field). I do this:

grep -oP 'SRC=([^ ]+).+SPT=([0-9]+)' /var/log/iptables.log

但它返回:

SRC=1.1.1.1 DST=2.2.2.2 LEN=40 TOS=0x00 PREC=0x00 TTL=57 ID=0 DF PROTO=TCP SPT=80

我如何得到$ 1和$ 2只(参考匹配的值)?

How do I get $1 and $2 only ( reference to the matched value)?

推荐答案

A SED 办法:

sed -rn 's/.*SRC=([^ ]+).*SPT=([0-9]+).*/\1 \2/p' /var/log/iptables.log

您可以通过管道将它,而在你的脚本或类似阅读的src SPT 。现在,这当然不是很有效,因为三星的格局,因此,如果性能是一个问题,您可以考虑使用像剪切的东西来提取某些字段:

You can pipe it to while read src spt in your script or something similar. Now this of course is not very efficient, because of three stars in the pattern, so if performance is an issue, you can consider using things like cut to extract certain fields:

cut -d' ' -f12,21 /var/log/iptables.log

不知道,如果记录格式是一致的就好了这一工作。

Not sure if the log format is consistent enough for this to work.

这篇关于grep和打印反向引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆