I wanted to write a python script that gives me the difference between two txt files that contain list of domains.
Below is my script that I was able to create.
The output I receive from the above code is as follows:
The output I'm expecting is that any line that starts with a
Eventually, the group of "New DNS Entries" should display their appropriate DNS resolutions.
Something like this..
Below is my script that I was able to create.
import difflib file1 = open("2.txt").readlines() file2 = open("diff.txt").readlines() with open ('diff.txt', 'w', encoding = 'UTF8') as diff: for line in difflib.unified_diff(file2,file1,n=0): for prefix in ('---','+++','@@'): if line.startswith(prefix): break if line.startswith('+'): diff.write("New DNS Entry:\n{}\n".format(line[1:])) elif line.startswith('-'): diff.write("Removed DNS Entry:\n{}\n".format(line[1:]))Since
unified_diff
produces context and characters like +++, --- & @@, I have modified my code to omit this and disabled context. The output I receive from the above code is as follows:
Output:Removed DNS Entry:
--
New DNS Entry:
++
Removed DNS Entry:
example.com
As seen above, the output titles anything with a -
as "Removed DNS Entry" and anything with a +
as "New DNS Entry" and stops at very first iteration of a line change between two txt files (although there are multiple changes beyond example.com)The output I'm expecting is that any line that starts with a
+
should be grouped together and titled "New DNS Entries" and similarly any line that starts with -
should be grouped together and titled "Removed DNS Entries"Eventually, the group of "New DNS Entries" should display their appropriate DNS resolutions.
Something like this..
Output:New DNS Entries:
example1.com
example2.com
Removed DNS Entries:
example13.com
example21.com
DNS Resolution for new entries:
example1.com CNAME example.aws.com
example2.com A 192.168.1.1
Any help here is much appreciated!