I have two txt files and need to get the output in a new txt file :
one file has the below (named f.txt):
JavaScript
x
10
10
1
interface Eth-Trunk50
2
interface Eth-Trunk51
3
interface Eth-Trunk60
4
interface Eth-Trunk60.2535
5
interface Eth-Trunk100
6
interface GigabitEthernet0/0/0
7
interface GigabitEthernet1/1/1
8
interface GigabitEthernet1/1/4
9
interface GigabitEthernet1/1/10
10
and another file has the below lines (named x.txt):
JavaScript
1
17
17
1
interface Eth-Trunk50.1000
2
interface Eth-Trunk50.1030
3
interface Eth-Trunk51.2071
4
interface Eth-Trunk51.2072
5
interface Eth-Trunk100.108
6
interface Eth-Trunk100.109
7
interface Eth-Trunk100.111
8
interface GigabitEthernet1/1/0
9
interface GigabitEthernet1/1/1.660
10
interface GigabitEthernet1/1/1.662
11
interface GigabitEthernet1/1/2
12
interface GigabitEthernet1/1/10.3400
13
interface GigabitEthernet1/1/10.3600
14
interface GigabitEthernet1/1/10.3800
15
interface GigabitEthernet2/1/1
16
interface GigabitEthernet2/1/1.3010
17
i need to get the lines of f.txt file which not the first part or not exist in x.txt lines to get the output like below :
JavaScript
1
5
1
interface Eth-Trunk60
2
interface Eth-Trunk60.2535
3
interface GigabitEthernet0/0/0
4
interface GigabitEthernet1/1/4
5
and here is my python code but its not working for me , so thanks to help please .
JavaScript
1
15
15
1
def readB(Devices):
2
nums = []
3
with open('C:/Users/Ahmed Shouaib/Downloads/Backup Configurtion files/'+Devices+'-NO trust upstream default33.txt') as f:
4
nums = f.read().split('n')
5
nums = [n.strip() for n in nums if n !='']
6
with open('C:/Users/Ahmed Shouaib/Downloads/Backup Configurtion files/'+Devices+'-NO trust upstream default55.txt') as x , open('C:/Users/Ahmed Shouaib/Downloads/Backup Configurtion files/' + Devices + '-NO trust upstream default66.txt','w') as new :
7
for n in nums:
8
for line in x :
9
if not line.startswith(n) :
10
print (n)
11
new.write(n+'n')
12
break
13
14
readB('10.0.130.13')
15
Advertisement
Answer
read all line from both files
remove the last
'n'
of each linecheck whether the line is valid, use a
valid
flag here :
initialize
valide
flag as ‘True’if any line is start with the checking line, mark the
valid
flag as ‘False’filter lines by the
valid
flag
- write the result to the result file
JavaScript
1
20
20
1
def readB(Devices):
2
fLines = open('./f.txt').readlines()
3
xLines = open('./x.txt').readlines()
4
with open('./result.txt', 'w') as new:
5
for fLine in fLines:
6
fLine = fLine.replace('n', '')
7
8
valid = True
9
for xLine in xLines:
10
xLine = xLine.replace('n', '')
11
if xLine.startswith(fLine):
12
valid = False
13
break
14
if valid:
15
print(fLine)
16
new.write(fLine + 'n')
17
18
19
readB('10.0.130.13')
20