如何从文本文件中提取超过 20 个重复的 IP 地址
How to extract more than 20 duplicate IP addresses from a text file
我有一个 .txt 事件列表,我需要制作一个脚本来比较列表中的 IP,如果 IP 在不同行中出现超过 15 次,则打印它在其中看到的行, <15 的可以忽略。
.txt 数据如下所示:
11/08/2019 07:47 192.168.14.14 tcp/20542 tcp/23 192.168.175.141
11/08/2019 07:55 192.168.98.105 tcp/38155 tcp/5555 192.168.170.188
11/08/2019 08:17 192.168.227.10 tcp/2739 tcp/8080 192.168.162.230
11/08/2019 08:32 192.168.74.26 tcp/52243 tcp/5555 192.168.187.234
11/08/2019 08:14 192.168.74.26 tcp/58019 tcp/5555 192.168.176.132
11/08/2019 08:14 192.168.74.26 tcp/58019 tcp/5555 192.168.176.132
11/08/2019 08:14 192.168.74.26 tcp/58019 tcp/5555 192.168.176.132
我该怎么做?
from collections import Counter
with open('3.txt') as file:
c=Counter(c.strip().lower() for c in file if c.strip())
if c[line[17:31]]>20:
print (line)
如果 IP 在行中出现超过 20 次,结果应如下所示:
11/08/2019 07:55 192.168.98.105 tcp/38155 tcp/5555 192.168.170.188
这是一种使用 collections.defaultdict
的方法。
例如:
from collections import defaultdict
check_val = defaultdict(int)
with open('3.txt') as file:
for line in file:
line = line.strip().split()
if check_val[line[-1]] > 15:
print(line)
else:
check_val[line[-1]] += 1
我有一个 .txt 事件列表,我需要制作一个脚本来比较列表中的 IP,如果 IP 在不同行中出现超过 15 次,则打印它在其中看到的行, <15 的可以忽略。
.txt 数据如下所示:
11/08/2019 07:47 192.168.14.14 tcp/20542 tcp/23 192.168.175.141
11/08/2019 07:55 192.168.98.105 tcp/38155 tcp/5555 192.168.170.188
11/08/2019 08:17 192.168.227.10 tcp/2739 tcp/8080 192.168.162.230
11/08/2019 08:32 192.168.74.26 tcp/52243 tcp/5555 192.168.187.234
11/08/2019 08:14 192.168.74.26 tcp/58019 tcp/5555 192.168.176.132
11/08/2019 08:14 192.168.74.26 tcp/58019 tcp/5555 192.168.176.132
11/08/2019 08:14 192.168.74.26 tcp/58019 tcp/5555 192.168.176.132
我该怎么做?
from collections import Counter
with open('3.txt') as file:
c=Counter(c.strip().lower() for c in file if c.strip())
if c[line[17:31]]>20:
print (line)
如果 IP 在行中出现超过 20 次,结果应如下所示:
11/08/2019 07:55 192.168.98.105 tcp/38155 tcp/5555 192.168.170.188
这是一种使用 collections.defaultdict
的方法。
例如:
from collections import defaultdict
check_val = defaultdict(int)
with open('3.txt') as file:
for line in file:
line = line.strip().split()
if check_val[line[-1]] > 15:
print(line)
else:
check_val[line[-1]] += 1