I want to compare lines in two files, but to minimize noise in the output, I want only the actual differences in the lines to be printed.
For instance, given the two files below:
a.txt
a b c d e f g h i j k l m n o p q r s t u v w x y z
b.txt
a B c d e f g h i j k l m n o p q r s t u v w x y z
(the difference between them is the case of letter b
)
I want the output to be something like:
[-b-]{+B+}
Currently, the best approach I found was to use git diff --word-diff
, but it outputs the whole line:
a [-b-]{+B+} c d e f g h i j k l m n o p q r s t u v w x y z
Is there a more direct way to do it, other than manually parsing the output? Also, ideally I would prefer to use something more commonly available than git diff
, e.g. a POSIX shell tool that would not require the user to install extra packages.
1 Answer 1
Using wdiff:
$ wdiff -3 a.txt b.txt
======================================================================
[-b-] {+B+}
======================================================================
The -3
or ---no-common
option will remove words that are common between the two files and only show the differences.
The ===...
banner (and empty lines) may be removed with grep
:
$ wdiff -3 a.txt b.txt | grep -vx '=*'
[-b-] {+B+}
wdiff
may also read unified diff
data if you give it the -d
or --diff-input
option, for example from git
:
git diff somefile | wdiff -d -3
Although wdiff
is not a POSIX tool, it is commonly available.
-
2It might be worth noting that if your terminal supports ANSI escapes, you can make wdiff print fancy colored output that's (imo) easier to read with this in your bashrc:
alias wdiff="wdiff -n -w $'033円[30;41m' -x $'033円[0m' -y $'033円[30;42m' -z $'033円[0m'"
(taken from here).scohe001– scohe0012019年01月18日 17:18:13 +00:00Commented Jan 18, 2019 at 17:18
b
andB
so it's obvious? I understand that this was probably the actual difference, but for purposes of the question you can make it easier.