我有两个IP列表。我想验证每一对都存在于文件的同一行:ips_template.txt
。如果文件ips_template.txt
中不存在IP或它们不是“对”,则打印不匹配的IP。在bash
中,我只需要通过管道传输两个grep
,寻找具有相同结果的东西。你知道吗
firstIPs = re.findall(r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b", firststring)
secondIPs = re.findall(r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b", secondstring)
if firstIPs or secondIPs :
print 'one of the lists didn\'t had IPs.'
sys.exit(2)
if len(firstIPs ) != len(secondIPs ):
print 'IPs len mismatch'
sys.exit(2)
for old, new in zip(firstIPs , secondIPs ):
# bash example to search in the file ips_template.txt
# if [ `cat ips_template.txt | grep old | grep new | wc -l` -gt 0 ]
# echo 'match'
# else
# echo ips not matched or missing
# fi
ips_template.txt
exmaple公司:
hostname 1.1.1.1 2.2.2.2 hostname_type
hostname2 1.1.1.1 2.2.2.2 hostname_type2
firststring
和secondstring
可以是不同的格式,有些是未知的。这就是为什么我只从这些IP中剥离IP,并假设IP/主机的顺序是相同的。你知道吗
要在
ips_template.txt
中搜索IP,我执行了以下操作:另外,在检查列表中是否包含IP时,我插入了两个
not
:因为它只说“其中一个列表没有IP”,而其中至少有一个包含IP
我的完整代码是:
ips_template.txt
看起来像这样:我的代码输出是:
相关问题 更多 >
编程相关推荐