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


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")


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

and name this script whatever you want (needs Python 2.x to execute, like diff2html):

import sys


first_c = ""
for n in range(len(f1_read)):
    if f1_read[n]!=f2_read[n]:
        if 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

  • Windows or Linux, but Windows is prefered – PythEch Apr 15 '12 at 13:34
  • 1
    Now, can you give us an example input and how you want the output of the comparison to look like? (Please edit your question) – slhck Apr 15 '12 at 13:37
  • what happens if you use diff -a (or, the same diff --text) ? – lupincho Apr 15 '12 at 14:33
  • Nothing changes – PythEch Apr 15 '12 at 14:47
  • 1
    You could try "Beyond Compare". – Daniel R Hicks Apr 15 '12 at 21:16

diff assumes that the second file may have been created by not only editing lines but by inserting and deleting lines.

If you just want to know which lines differ without considering inserted or deleted lines, you can use a simpler approach.

$perl d.pl a b | less
  1:   255,   107 DIFFERENT!
  2:   197,   148 DIFFERENT!
  3:   113,   165 DIFFERENT!
  4:     0,   221 DIFFERENT!
  5:     0,   153 DIFFERENT!
  6:     0,    85 DIFFERENT!
  7:    48,   255 DIFFERENT!
  8:    61,   187 DIFFERENT!
  9:    63,    85 DIFFERENT!
 10:     3,     0 DIFFERENT!
 11:    49,     0 DIFFERENT!
 12:    58,    15 DIFFERENT!
 13:    47,     0 DIFFERENT!
 14:    62,     0 DIFFERENT!
 15:    61,     0 DIFFERENT!
 16:   255,   255 same
 17:   255,   255 same
 18:   255,   255 same
 19:   255,   255 same
 20:   255,   255 same
 21:   255,   255 same

where d.pl is

use strict;
use warnings;

open my $f1, '<', $ARGV[0] or die "Can't read '$ARGV[0]' - $!\n";
open my $f2, '<', $ARGV[1] or die "Can't read '$ARGV[1]' - $!\n";

while (my $f1_line = <$f1>) {
  my $f2_line = <$f2>;
  chomp for $f1_line, $f2_line;
  my $verdict = $f1_line eq $f2_line ? 'same' : "DIFFERENT!";
  printf "%3d: %5d, %5d %s\n", $., $f1_line, $f2_line, $verdict;

close $f2;
close $f1;
| improve this answer | |
  • I almost finished my Python script that imitates diff-utils :) But I will accept this as an answer! – PythEch Apr 15 '12 at 15:19

To compare files side-by-side, my favorite tool is Notepad++.

After installing it, on the toolbar click Plugins>Plugin Manager>Available, and check Compare and Install.

After restarting Notepad++, open up the two files (they will open in different tabs), and in the toolbar go to Plugins>Compare>Compare.

There are multiple tweaks you can do, but comparing files looks something like this:

enter image description here

where white text is unchanged, red is removed, green is added, and the sidebar shows an overview of those changes in the two files.

| improve this answer | |
  • Good point! I never knew that Notepad++ has that ability! But it gives the same results. I'm thinking to make my own script that imitates diff output and then I will use diff2html again. – PythEch Apr 15 '12 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.