Comparing 2 (or 3 Files If Possible) "Line By Line"

Posted by PythEch on Super User See other posts from Super User or by PythEch
Published on 2012-04-15T13:30:57Z Indexed on 2012/04/15 17:35 UTC
Read the original article Hit count: 288

Filed under:
|
|
|

I want to find out the differences of 2 (or 3 files if possible) line by line. Diff utils can do this, however it gives inaccurate results. Because, 2 files have exact number of lines which is "134". But diff gives me "Added Lines" and "Removed Lines". However this is wrong, they have exact the same number of lines, there is no added or removed lines.

The text files which I want to find differences of them, have only numbers written, maybe that's why that algortihm fails. I couldn't find any option to prevent that, however I may be wrong, I mean there should be an option for that, but again, I couldn't find.

This is what I get (5am.txt vs 6am.txt, there is a huge problem): http://img26.imageshack.us/img26/7049/wrongp.png

This is what I want (6am.txt vs 7am.txt, still has problems): http://img402.imageshack.us/img402/5421/semiright.png

But, first the first image still has this problem, at the last lines.

Edit:

After I figured out that there is no utility to do this, I handled myself. I almost did the same thing as what RedGrittyBrick have done. This script imitates diff utility so I (or you) can use it with diff2html.

To use it with diff2html, just change line

diff_stdout = os.popen("diff %s" % string.join(argv[1:]), "r")

to

diff_stdout = os.popen("script.py %s" % string.join(argv[1:]), "r")

and name this script whatever you want:

import sys

f1=open(sys.argv[1],"r")
f1_read=f1.readlines()
f1.close()
f2=open(sys.argv[2],"r")
f2_read=f2.readlines()
f2.close()

changed={}
first_c = ""
for n in range(len(f1_read)):
    if f1_read[n]!=f2_read[n]:
        if first_c == "":
            first_c=n+1
        changed[first_c]=n+1
    else:
        first_c=""

#Let's imitate diff-utils...

for (x, y) in changed.items():
    print "%d,%dc%d,%d" % (x,y,x,y)
    for i in range(x,y+1):
        sys.stdout.write("< %s" % f1_read[i-1])
    print "---"
    for i in range(x,y+1):
        sys.stdout.write("> %s" % f2_read[i-1])

Final results: enter image description here

© Super User or respective owner

Related posts about Windows

Related posts about linux