50

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.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
nomazoma49
  • 525
  • 1
  • 4
  • 6

4 Answers4

36

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 '($'  
Utsav
  • 5,572
  • 2
  • 29
  • 43
  • 8
    @ Utsav: I want the file numbers present of the file itself and not the line numbers generated by cat -n. Lets say, i am doing diff using "--suppress-common-lines" then the line numbers should be omitted which are not shown in the diff. The line format parameter works with -u only. It gives a "conflict" exception when use with diff -y or sdiff. – nomazoma49 Aug 16 '16 at 15:44
  • 3
    diff --unchanged-line-format="" --old-line-format=":%dn: %L" --new-line-format=":%dn: %L" file1 file2 – gbonetti Apr 04 '20 at 09:37
  • My file has linefeeds and carriage returns which corrupted the behavior of the ```cat -n``` portion. It was overwriting itself so the left-hand data was spanning to the right side. I solved this with: ```sdiff -l file1 file2 | tr \\r \ | cat -n | grep -v -e '($``` This replaces the carriage return \r with a space. – Brent K. Mar 01 '22 at 17:44
7
sdiff -s <(cat -n file1.txt) <(cat -n file2.txt)

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

Mike Titus
  • 89
  • 1
  • 1
  • 3
    This is less than ideal, as the line numbering is being applied *before* the diff, which means that additions/deletions will cause diff to think the line numbers are changes in the file. E.g. with your method a file with "A/B/X/C/D" (where "/" is a linebreak) diff-ed against "B/C/X/D" will show all lines except the X changing, whereas ideally it should be showing the X moving and A being deleted (and the rest being unchanged). – R.M. May 02 '17 at 23:36
  • I needed something similar to what the OP asked, and this was the only answer that gave me a useful option. Sure, it may need improvement so the diff works in all or maybe even more cases. But I think this answer is _sufficiently_ useful and correct even now to deserve being the accepted answer. If the OP agrees, can he/she change the accepted answer? – anuragw Aug 19 '17 at 11:21
6

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 '($'
markpietrus
  • 361
  • 3
  • 5
0

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