Posts

Showing posts from January, 2017

Find difference between two text files in Python

Image
#!/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) This script compares two text files in Python using difflib and prints a unified diff, the same format version control tools use. You pass the two file paths as arguments. difflib ships with the standard library, so nothing needs installing, and it is handy for comparing generated output, logs, or configuration against an expected version.

Find difference between two text files in Perl

Image
#!/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"; } This Perl script compares two text files and prints their differences using the Text::Diff module. You pass the two file paths as arguments and it reports the changed lines, much like the Unix diff command. Doing this in code is useful inside larger pipelines, for example checking generated output against an expected baseline or flagging configuration drift between environments.