Python Forum
Python Script to Produce Difference Between Files and Resolve DNS Query for the Outpu - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python Script to Produce Difference Between Files and Resolve DNS Query for the Outpu (/thread-18539.html)



Python Script to Produce Difference Between Files and Resolve DNS Query for the Outpu - sultan - May-22-2019

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.

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!


RE: Python Script to Produce Difference Between Files and Resolve DNS Query for the Outpu - heiner55 - May-22-2019

If difflib does not work for you,
then do it the old way:

Read file1 into memory and sort the lines.
Read file2 into memory and sort the lines.
Compare line by line.


RE: Python Script to Produce Difference Between Files and Resolve DNS Query for the Outpu - buran - May-22-2019

(May-22-2019, 03:42 AM)heiner55 Wrote: Read file1 into memory and sort the lines.
Read file2 into memory and sort the lines.
Compare line by line.

actually loading each file into set and using set operations would be simpler