[bash] unix diff side-to-side results?

How can I plot the results of a unix diff command side-to-side instead of one difference after the other? See below for an example:

    diff /tmp/test1  /tmp/test2
1,4c1,2
< asfdsadf
< asdfsad
< fsaf
< fdsadf
---
> asdfsafdsf
> saf
6,8d3
< sadf
< asdf
< sadf
10d4
< fasd
12,13c6,14
< sadfa
< fd
---
> sadf
> sadf
> sadf
> sadf
> sadf
> sadf
> sadf
> sadf
> safa

I would like to have something like:

diff /tmp/test1  /tmp/test2
1,4c1,2
< asfdsadf       > asdfsafdsf
< asdfsad        > saf       
< fsaf
< fdsadf
---
6,8d3
< sadf
< asdf
< sadf
10d4
< fasd
12,13c6,14
< sadfa               > sadf
< fd              > sadf
---               > sadf
              > sadf
              > sadf
              > sadf
              > sadf
              > sadf
              > safa

This question is related to bash unix diff

The answer is


Use the -y option:

diff -y file1 file2

From icdiff's homepage:

enter image description here

Your terminal can display color, but most diff tools don't make good use of it. By highlighting changes, icdiff can show you the differences between similar files without getting in the way. This is especially helpful for identifying and understanding small changes within existing lines.

Instead of trying to be a diff replacement for all circumstances, the goal of icdiff is to be a tool you can reach for to get a better picture of what changed when it's not immediately obvious from diff.

IMHO, its output is much more readable than diff -y.


Try cdiff - View colored, incremental diff in workspace or from stdin with side by side and auto pager support.


You should have sdiff for side-by-side merge of file differences. Take a read of man sdiff for the full story.


You can use vimdiff.

Example:

vimdiff file1 file2

You can simply use:

diff -y fileA.txt fileB.txt | colordiff

It shows the output splitted in two colums and colorized! (colordiff)


You can use:

sdiff  file1 file2

or

diff -y file1 file2

or

vimdiff file1 file2

for side by side display.


diff -y --suppress-common-lines file1 file2

Enhanced diff command with color, side by side and alias

Let's say the file contents are like:

cat /tmp/test1.txt
1
2
3
4
5
8
9

and

cat /tmp/test2.txt
1
1.5
2
4
5
6
7

Now comparing side-by-side

diff --width=$COLUMNS --suppress-common-lines --side-by-side --color=always /tmp/test1.txt /tmp/test2.txt
                                                                              > 1.5
3                                                                             <
8                                                                             | 6
9                                                                             | 7

You can define alias to use

alias diff='diff --width=$COLUMNS --suppress-common-lines --side-by-side --color=always'

Then new diff result:

diff /tmp/test1.txt /tmp/test2.txt
                                                                              > 1.5
3                                                                             <
8                                                                             | 6
9                                                                             | 7

If your files have inconsistent use of spaces and tabs, you may find it helpful to include the -t argument to expand the tabs:

diff -ty file1 file2

Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to unix

Docker CE on RHEL - Requires: container-selinux >= 2.9 What does `set -x` do? How to find files modified in last x minutes (find -mmin does not work as expected) sudo: npm: command not found How to sort a file in-place How to read a .properties file which contains keys that have a period character using Shell script gpg decryption fails with no secret key error Loop through a comma-separated shell variable Best way to find os name and version in Unix/Linux platform Resource u'tokenizers/punkt/english.pickle' not found

Examples related to diff

Create patch or diff file from git repository and apply it to another different git repository Comparing the contents of two files in Sublime Text Git diff between current branch and master but not including unmerged master commits Fast way of finding lines in one file that are not in another? Python - difference between two strings How to see the changes in a Git commit? unix diff side-to-side results? Find the files existing in one directory but not in the other git diff between two different files How to get the difference (only additions) between two files in linux