[英]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.