I wanted to format the Unix files conditionally, I am currently working on diff
command and wanted to know if it is possible to format the text of the diff
command output.
Example:
Matched values should be displayed in green.
Unmatched values should be displayed in red.
Suppose I have two files file1
and file2
and my command is diff file1 file2
.
Now I wanted that suppose output contain 5 mismatch then those mismatch should be displayed in Red color. How to achieve this using unix?
In short "Change color to red for the output of diff command for values which mismatch"
Best Answer
diff --color
option was added to GNU diffutils 3.4 (2016-08-08)This is the default
diff
implementation on most distros, which will soon be getting it.Ubuntu 18.04 has
diffutils
3.6 and therefore has it.On 3.5 it looks like this:
Tested:
Apparently added in commit c0fa19fe92da71404f809aafb5f51cfd99b1bee2 (Mar 2015).
Word-level diff
Like
diff-highlight
. Not possible it seems, feature request: https://lists.gnu.org/archive/html/diffutils-devel/2017-01/msg00001.htmlRelated threads:
ydiff
does it though, see below.ydiff
side-by-side word level diffhttps://github.com/ymattw/ydiff
Is this Nirvana?
Outcome:
If the lines are too narrow (default 80 columns), fit to screen with:
Contents of the test files:
a
b
ydiff
Git integrationydiff
integrates with Git without any configuration required.From inside a git repository, instead of
git diff
, you can do just:and instead of
git log
:See also: https://stackoverflow.com/questions/7669963/how-can-i-get-a-side-by-side-diff-when-i-do-git-diff/14649328#14649328
Tested on Ubuntu 16.04, git 2.18.0, ydiff 1.1.