导入重新 来自itertools import islice dnlist = [] maclist = []
以open('bulkdn.txt','r')作为行: 换行: 如果'@ttml'排成一行: dn = re.findall(r'\ d {10}',line) dnlist.append(DN) 如果'svc:1'排成一行: mac = re.findall(r'mac:[a-fA-F0-9] {2} [:] [a-fA-AF0-9] {2} [:] [a-fA-F0-9] { 2} [:] [A-FA-F0-9] {2} [:] [A-FA-F0-9] {2} [:] [A-FA-F0-9] {2}”,线) maclist.append(MAC)
i=0 #next
for dn,mac in zip(dnlist,maclist):
print('{} {}{}'.format(i,dn,mac))
i+=1
输入文件如下
用户名 DESCR。
- svc:1 sap:lag-50:1776.2000 mac:00:17:7c:75:99:75 sid:5421
svc:1 sap:lag-50:2680.2000 mac:00:17:7c:6d:d6:2b sid:4099
svc:1 sap:lag-50:2805.2000 mac:00:17:7c:80:e2:fb sid:1796
svc:1 sap:lag-50:2813.2000 mac:e4:6f:13:c0:13:9a sid:2420
svc:1 sap:lag-50:1715.3615 mac:10:62:eb:66:24:sid:6612
svc:1 sap:lag-50:62.3602 mac:00:17:7c:83:51:24 sid:902
svc:1 sap:lag-50:65.3605 mac:00:17:7c:7c:fa:f9 sid:1200
svc:1 sap:lag-50:1714.3614 mac:78:32:1b:99:a0:f9 sid:1178
svc:1 sap:lag-50:2585.2000 mac:00:17:7c:7c:24:42 sid:7710 0d 00:00:01 o当地N / A. 0200200003 @ TTML svc:1 sap:lag-50:1775.2000 mac:00:17:7c:78:74:56 sid:7151 0d 01:08:06 oE local 114.143.122.39 0200200006 @ TTML svc:1 sap:lag-50:2851.3739 mac:00:17:7c:77:ab:26 sid:2664 0d 07:26:23 oE local 49.248.206.250 01:4A:83:6A:03:4A:83:6A 0200200008 @ TTML svc:1 sap:lag-50:79.3609 mac:80:26:89:ca:62:8b sid:4849 0d 00:28:31 oE local 49.248.169.78 82:26:89:FF:FE:CA:62:8B 0200200015 @ TTML svc:1 sap:lag-50:73.3603 mac:6c:72:20:49:8b:6e sid:7559 0d 02:28:27 oE local 49.248.177.166 6E:72:20:FF:FE:49:8B:6E 0200200016 @ TTML svc:1 sap:lag-50:1720.3620 mac:00:17:7c:7d:41:22 sid:7539 0d 02:52:09 oE local 49.248.48.145
预期产量是 0200200015 mac:6c:72:20:49:8b:6e 0200200008 mac:80:26:89:ca:62:8b 0200200016 mac:00:17:7c:7d:41:22
答案 0 :(得分:0)
我忽略了这样一个事实,即您的代码格式不符合您的预期,因为我认为它是与您要问的问题dn不匹配的mac地址。
基本上你匹配mac地址和dns,但根据你定义的正则表达式,有14个mac和5个dns。
我会同时使用这样的多行正则表达式匹配dn和mac。我还没有对这个正则表达式进行优化,但是它尽可能地保持冗长,所以它不言自明。基本上,虽然它匹配dn,文本的各种静态位然后是mac形字符串。我们找到了所有这些,这就是答案!
import re
with open('bulkdn.txt','r') as f:
text = f.read()
r = re.compile(r"^([0-9]{10})\@ttml$\s*svc:1\s.*\smac:([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})", re.MULTILINE)
ms = r.findall(text)
for m in ms:
print(m)
结果:
('0200200003', '00:17:7c:78:74:56')
('0200200006', '00:17:7c:77:ab:26')
('0200200008', '80:26:89:ca:62:8b')
('0200200015', '6c:72:20:49:8b:6e')
('0200200016', '00:17:7c:7d:41:22')