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.