How to display line numbers in side by side diff in unix?

54.1k Views Asked by At

The scenario is that i have 2 files which i want to diff side by side using the following command with the line numbers:

diff -y file1.txt file2.txt

and

sdiff file1.txt file2.txt

The above command just prints the side by side diff but doesn't display the line numbers. Is there any way to do it ? I searched a lot but couldn't find any solutions. I can't use third party tools FYI. Any genius ideas from anyone ?

Update:

I want the file numbers present of the file itself and not the line numbers generated by piping to cat -n etc.. Lets say, i am doing diff using "--suppress-common-l‌​ines" then the line numbers should be omitted which are not shown in the diff.

4

There are 4 best solutions below

3
On BEST ANSWER

Below code can be used to display the uncommon fields in two files, side by side.

sdiff -l file1 file2 | cat -n | grep -v -e '($'  

Below code will display common fields along with line numbers in the output.

diff -y file1 file2 | cat -n | grep -v -e '($'  
2
On
sdiff -s <(cat -n file1.txt) <(cat -n file2.txt)

This gives you side-by-side output with line-numbers from the source files.

0
On

The following command will display the side-by-side output prepended with line numbers for file1.txt and identical lines removed.

sdiff -l file1.txt file2.txt | cat -n | grep -v -e '($'
0
On

I had the same issue and ended up using a graphical tool (diffuse) under fedora 28