Sunday, January 08, 2017

Find difference between two text files in Python

#!/usr/bin/python
import difflib, sys

file1 = sys.argv[1]
file2 = sys.argv[2]

diff = difflib.unified_diff ( open(file1).readlines(), open(file2).readlines(), fromfile=file1, tofile=file2 )

strings = list()

for line in diff:
    strings.append(line.replace('\n', ''))

for string in strings:
    print(string)

Find difference between two text files in Perl

#!/usr/bin/env perl
use strict;
use warnings;
use Text::Diff;

my $file1 = $ARGV[0];
my $file2 = $ARGV[1];

my $diffs = diff $file1,$file2;
my @strings = split("\n",$diffs);

foreach(@strings)
{
    print $_."\n";
}