我的测试文件如下所示
输入
$ cat file
date="2017-10-10" ip=192.168.1.1:22 inbound=100 outbound=50
date="2017-10-10" ip=192.168.1.1:22 inbound=100
date="2017-10-10" ip=192.168.1.1:22 outbound=60
我使用下面的awk命令为inbound =。
提取值$ awk '{found=0; for(i=1;i<=NF;i++) { if ($i ~ /inbound/) { split($i,arr,"="); print arr[2];found=1 } } if (!found) print 0 }' file
100
100
0
现在我想将“IP”,“port”和“outbound”添加到列表中,以便我的输出看起来像
IP,PORT,INBOUND,OUTBOUND
192.168.1.1,22,100,50
192.168.1.1,22,100,0
192.168.1.1,22,0,60
答案 0 :(得分:0)
怎么样?
awk '{
ip=0;
port=0;
inbound=0;
outbound=0;
for(i=1;i<=NF;i++) {
if ($i ~ /ip=/) {
split($i,arr,"=");
split(arr[2],arr2,":");
ip = arr2[1];
port = arr2[2];
}
if ($i ~ /inbound/) {
split($i,arr,"=");
inbound=arr[2];
}
if ($i ~ /outbound/) {
split($i,arr,"=");
outbound=arr[2];
}
}
print ip "," port "," inbound "," outbound
}' file