Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 841
nomazoma49 Avatar asked Aug 15 '16 07:08

nomazoma49


People also ask

How can you tell less to display line numbers?

From the manual: -N or --LINE-NUMBERS Causes a line number to be displayed at the beginning of each line in the display. You can also toggle line numbers without quitting less by typing -N . It is possible to toggle any of less's command line options in this way.

What is the output of diff command in Unix?

On Unix-like operating systems, the diff command analyzes two files and prints the lines that are different. In essence, it outputs a set of instructions for how to change one file to make it identical to the second file.


1 Answers

Below code can be used display 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 '($'   
like image 153
Utsav Avatar answered Oct 17 '22 07:10

Utsav