我有一个文件,每行包含一个CIDR地址和端口:
192.168.1.0/24 3306
192.168.1.0/24 55982
10.10.10.0/24 5800
10.10.10.0/24 39690
10.10.10.0/24 50112
192.168.1.0/24 3308
192.168.1.0/24 3312
192.168.1.0/24 3316
使用GNU Awk 4.1.4(在Linux系统上),最初的想法是将所有端口放在一行上按CIDR地址分组,所以这样做了:
awk '{a[$1]=a[$1] ? a[$1]","$2 : $2} END {for (j in a) {print j" over ports: "a[j]}}' file
192.168.1.0/24 over ports: 3306,3308,3312,3316,55982
10.10.10.0/24 over ports: 5800,39690,50112
我还想为每个CIDR地址添加一个计数,以获得此结果:
192.168.1.0/24 over 5 ports: 3306,3308,3312,3316,55982
10.10.10.0/24 over 3 ports: 5800,39690,50112
我已经尝试将值放入数组并打印出密钥,但计数总是达到2。
awk '{a[$1]=a[$1] ? a[$1]","$2 : $2} {h[$1]++;} END {for (i in h) for (j in a) {print j" over "h[i]" ports: "a[j]}}' file
192.168.1.0/24 over 2 ports: 3306,3308,3312,3316,55982
10.10.10.0/24 over 2 ports: 5800,39690,50112
除了awk之外我还愿意做其他事情,但是由于带有CIDR /端口的文件数百万行,而且awk很快,所以它更可取。有什么想法吗?
答案 0 :(得分:2)
这是awk
的解决方案:
awk '{ z[$1][++count[$1]]; s[$2]=$1; }
END {
for (i in z)
{
printf( i" over "count[i]" ports: " );
c=0;
# print ports according to the key value
# and the requested format, i.e. port1,port2,...
for (j in s)
if(s[j]==i)
# we don't want a comma at the end of the line,
# after the last port we put a newline
if (c<count[i]-1)
{
printf(j",");
c++;
}
else printf(j"\n")
}
}
}' file
及其输出:
10.10.10.0/24 over 3 ports: 5800,39690,50112
192.168.1.0/24 over 5 ports: 3306,3308,3312,3316,55982
答案 1 :(得分:2)
没有多暗阵列
@IBAction func facebookLogin(sender: AnyObject) {
AppUtility.loginWithFacebook { (success) in
if success {
AppUtility.getUserData({ (success) in
if success {
self.performSegueWithIdentifier("completeProfile", sender: nil)
ApplicationDelegate.showTabBarController()
}
else {
print ("Attempting FB Login - 5 ")
print ("Attempting FB Login - 6 ")
}
})
}
}
答案 2 :(得分:1)
$ awk '{cnts[$1]++; ports[$1]=($1 in ports ? ports[$1] "," : "") $2}
END{for (ip in ports) print ip, "over", cnts[ip], "ports:", ports[ip]}' file
10.10.10.0/24 over 3 ports: 5800,39690,50112
192.168.1.0/24 over 5 ports: 3306,55982,3308,3312,3316