[英]How to find the frequency of IP addresses that belong to the same subnet using regex?
我有一个巨大的json 文件,其中包含 IPv4 地址的条目。 假设 /24 子网掩码。 样本:
json = [
{ "ip": "154.16.58.206"},
{ "ip": "154.16.58.218"},
{ "ip": "154.16.46.180"},
{ "ip": "154.16.60.181"},
{ "ip": "154.16.46.167"},
{ "ip": "154.16.58.131"},
{ "ip": "154.16.60.173"},
{ "ip": "154.16.62.147"},
{ "ip": "154.16.62.175"},
{ "ip": "154.16.50.216"},
{ "ip": "154.16.58.141"}
]
const str = JSON.stringify(json)
我想要一些关于有哪些组以及每个组中有多少个 ip 的映射,例如:
{
"154.16.58.0" => 4
"154.16.46.0" => 2
"154.16.60.0" => 2
"154.16.62.0" => 2
"154.16.50.0" => 1
}
我也许能想出一些贪婪的 js 解决方案,但因为它的数据太多,我需要一个高性能的正则表达式解决方案。 我唯一能想到的就是/(\d+.\d+.\d+).\d+/g
假设file.json
包含:
[
{ "ip": "154.16.58.206"},
{ "ip": "154.16.58.218"},
{ "ip": "154.16.46.180"},
{ "ip": "154.16.60.181"},
{ "ip": "154.16.46.167"},
{ "ip": "154.16.58.131"},
{ "ip": "154.16.60.173"},
{ "ip": "154.16.62.147"},
{ "ip": "154.16.62.175"},
{ "ip": "154.16.50.216"},
{ "ip": "154.16.58.141"}
]
那么请您试试python
代码:
#!/usr/bin/python
import re, json
from collections import Counter
with open('file.json') as f:
net = []
for i in json.load(f):
ip = re.sub(r'\d+$', '0', i["ip"])
net.append(ip)
c = Counter(net)
print(json.dumps(c, indent=2))
Output:
{
"154.16.46.0": 2,
"154.16.50.0": 1,
"154.16.62.0": 2,
"154.16.60.0": 2,
"154.16.58.0": 4
}
我设法找到了一个适合我的解决方案:
const json = [
{ "ip": "154.16.58.206" },
{ "ip": "154.16.58.218" },
{ "ip": "154.16.46.180" },
{ "ip": "154.16.60.181" },
{ "ip": "154.16.46.167" },
{ "ip": "154.16.58.131" },
{ "ip": "154.16.60.173" },
{ "ip": "154.16.62.147" },
{ "ip": "154.16.62.175" },
{ "ip": "154.16.50.216" },
{ "ip": "154.16.58.141" }
]
const str = JSON.stringify(json)
const ipMap = new Map()
const regxp = /(\d+\.\d+\.\d+)\.\d+/g
const matches = str.matchAll(regxp);
for (const match of matches) {
const [, ip] = match
const val = ipMap.get(ip)
ipMap.set(ip, val ? val + 1 : 1)
}
console.log(ipMap)
Output:
(5) {
154.16.58 => 4
154.16.46 => 2
154.16.60 => 2
154.16.62 => 2
154.16.50 => 1
}
您可以在 JSitor 上试用: https://jsitor.com/-bUGo7lZC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.