你可以使用字典:
val = {}
result = ''
count = 1
content = """
abcdef 192.168.1.1
kbckdbc 10.10.10.10
abcdef 192.168.1.1
yuosdj 100.100.100.100
sadfsdf 192.168.1.1
newstuff 342.344.23.2
yuosdj 100.100.100.100
"""
data = [i.split() for i in filter(None, content.split('\n'))]
for a, b in data:
if b not in val:
result += f'{a} {count}\n'
val[b] = count
count += 1
else:
result += f'{a} {val[b]}\n'
print(result)
输出:
abcdef 1
kbckdbc 2
abcdef 1
yuosdj 3
sadfsdf 1
newstuff 4
yuosdj 3
编辑:要更新文件中的IP,可以使用
re
import typing, re
def change_ips(ips:typing.List[str]) -> typing.Generator[str, None, None]:
val = {}
count = 1
for i in ips:
if i not in val:
yield f'IP_{count}'
val[i] = count
count += 1
else:
yield f'IP_{val[i]}'
with open('filename.txt') as f:
content = f.read()
with open('filename.txt', 'w') as f1:
f1.write(re.sub('\d+\.\d+\.\d+\.\d+', '{}', content).format(*change_ips(re.findall('\d+\.\d+\.\d+\.\d+', content))))
2018-09-13 19:00:00,317 INFO -util.SSHUtil: Waiting for channel close
2018-09-13 19:00:01,317 INFO -util.SSHUtil: Waiting for channel close
2018-09-13 19:00:01,891 INFO -filters.BasicAuthFilter: Client IP:IP_1
2018-09-13 19:00:01,891 INFO -filters.BasicAuthFilter: Validating token ...
2018-09-13 19:00:01,892 INFO -authentication.Tokenization: Token:IP_1:20180913_183401is present in map
2018-09-13 19:00:01,892 INFO -configure.ConfigStatusCollector: status.
2018-09-13 19:00:01,909 INFO -filters.BasicAuthFilter: Client IP:IP_1
2018-09-13 19:00:01,909 INFO -filters.BasicAuthFilter: Validating token ...
2018-09-13 19:00:01,910 INFO -authentication.Tokenization: Token:IP_1:20180913_183401is present in map
2018-09-13 19:00:01,910 INFO -restadapter.ConfigStatusService: configuration status.
2018-09-13 19:00:01,910 INFO -configure.Collector: Getting configuration status.
2018-09-13 19:00:02,318 INFO -util.SSHUtil: Processing the ssh command execution results standard output.
2018-09-13 19:00:02,318 INFO -util.SSHUtil: Processing the ssh command execution standard error.
2018-09-13 19:00:02,318 INFO -util.SSHUtil: Remote command using SSH execution status: Host : [IP_2] User : [root] Password : [***********] Command : [shell ntpdate -u IP_3] STATUS : [0]
2018-09-13 19:00:02,318 INFO -util.SSHUtil: STDOUT : [Shell access is granted to root
14 Sep 01:00:01 ntpdate[16063]: adjust time server IP_3 offset 0.353427 sec]
2018-09-13 19:00:02,318 INFO -util.SSHUtil: STDERR : []
2018-09-13 19:00:02,318 INFO -util.SSHUtil: Successfully executedremote command using SSH.
2018-09-13 19:00:02,318 INFO Successfully executed the command on VCenter :IP_2