我有一个文件充满了这样的行,称为日志
19:56:52 12 Nov;Warning;em0;eth0;222.171.89.16;49.137.111.136;ICMP;;
08:35:51 00 Aug;That's odd;em0;eth0;142.53.155.238;252.1.134.24;ICMP;;
11:47:48 21 Jun;Look into this ;em1;eth0;50.219.1.59;56.95.45.60;UDP;16351;15354
我想使用 awk 找到包含我正在寻找的地址的行并重新排列它。
我的尝试是
awk '/222.171.89.16/ {print $2,$3,$6,$4,$5,$1}' logs
那只是给了我这条线,因为我想需要告诉 awk 如何分隔字符串,所以我尝试使用 FS
awk '/222.171.89.16/ {FS = ";"}; {print $2,$3,$6,$4,$5,$1}' logs
但这仅适用于某些行。另外,我想在开头说,如果 ip 地址来自第 5 列,则为出站,如果来自第 6 列,则为入站。所以数据应该是这个顺序。请原谅我也必须做的桌子。
TYPE | IFACE | OFACE | PROT | SPORT | DPORT | DATE |
|------+---------+---------+------+-------+-------+-----------------|
| OUT | em0 | eth0 | ICMP | 0 | 0 | 08:35:51 00 Aug |
| OUT | | virbr0 | TCP | 24760 | 26014 | 07:08:48 18 Feb |
| IN | em2 | | ICMP | 0 | 0 | 21:54:43 06 Aug |
| OUT | virbr0 | | UDP | 29450 | 2501 | 00:46:27 04 Aug |
| IN | virbr0 | eth1 | ICMP | 0 | 0 | 06:29:02 20 Sep |
| IN | em0 | em0 | ICMP | 0 | 0 | 15:41:37 09 Nov |
| OUT | eth0 | virbr0 | UDP | 21879 | 3645 | 13:43:33 11 Nov |
| IN | em1 | em1 | UDP | 7699 | 18698 | 06:06:15 12 Oct |
| OUT | em1 | em0 | ICMP | 0 | 0 | 14:11:09 25 Aug |
| IN | em2 | em0 | UDP | 24814 | 31182 | 17:45:57 00 Dec |
| OUT | eth1 | em1 | UDP | 4915 | 18665 | 01:49:46 15 Oct |
---------------------------------------------------------------------