2

I constantly need to compare couples of fixed-width text files which change on specific character positions. These files usually have a single line and a fixed number of bytes so most diff tools mark the whole line as different but don't specify which characters are the specific ones that divert.

Is there any tool specialized for this kind of files? I can only find line-wise tools. I don't mind the platform the tools runs on.

  • What operating system? – Nicolas Raoul Aug 15 '16 at 6:02
  • What kind of differences? Simple character replacements or are characters inserted or deleted as well? – angry person Aug 15 '16 at 10:47
2

With a couple of lines of python you can do this:

Python is pre-installed on most OS-X and Linux installations, can be installed on the remainder with apt-get and is available for Windows.

Example Code:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys

def diffline(l1, l2):
    """ Difference a pair of lines."""
    print("A:", l1.strip())
    print("B:", l2.strip())
    delta = ["   "]
    for n in xrange(min([len(l1), len(l2)])):
        if l1[n] == l2[n]:
            delta.append(' ')
        else:
            delta.append('^')
    excess = abs(len(l1) - len(l2))
    delta.append('^' * excess)
    print("".join(delta))


def linedelta(fname1, fname2):
    """
    Compare lines in two text files and mark the position of deltas.
    """
    print("A =", fname1)
    print("B =", fname2)
    line_no = 0
    l1 = "???"
    l2 = "!!!"
    with open(fname1, 'rt') as f1:
        with open(fname2, 'rt') as f2:
            while len(l1) and len(l2):
                line_no += 1
                l1 = f1.readline()
                l2 = f2.readline()
                if l1 <> l2:
                    print("Line:", line_no)
                    diffline(l1, l2)
    if not len(l1) + len(l2):
        print("Files differ after line", line_no)

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage:\n\tLineDelta.py file1 file2")
    else:
        linedelta(sys.argv[1], sys.argv[2])

Example inputs

Test1.txt:

ABCABCABCABCABCABCABCABCABCABCABCABC
CBACBACBACBACBACBACBACBACBACBACBACBA
ABCABCABCABCABCABCABCABCABCABCABCABC

Test2.txt:

ABCABCABCABCABCABCABCABCABCABCABCABC
CBACBACBACBACBACBACBACBACBACCACBACBA
ABCABCABCABCABCABCABCABCABCABCABCABC

Test Run

Running LineDelta.py Test1.txt Test2.txt gives:

A = test1.txt
B = test2.txt
Line: 2
A: CBACBACBACBACBACBACBACBACBACBACBACBA
B: CBACBACBACBACBACBACBACBACBACCACBACBA
                               ^

N.B. There are faster, shorter and more elegant implementations possible but I think that the above is probably more understandable.

1

Meld shows you changed characters in deep blue:

Meld character diff

Changed line are shown in light blue, so you can spot first the line then the character.

Gratis, open source.

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.