6

Is there a way to make the diff command look only for differences from a specified range of lines (from from line to to line), instead of the entire file? I am having difficulty trying to find the difference between two JavaScript functions in two files that are not on the same line. I could copy the range of lines into a new file, do the same for the other file, then compare them, but it would be tedious to do so especially since the files contain a lot of text.

I tried using:

diff "code1.js" "code2.js" --suppress-common-lines | tee outputFile

but it obviously does not show only the range of lines I am interested in comparing. It would be also useful if I could specify a range of lines to look for in one file and a different range of lines in the other file.

JAT86
  • 223

1 Answers1

12
diff <(sed -n 'S1,S2p' file1) <(sed -n 'S3,S4p' file2)

where

  • S1 is start line file1.
  • S2 is end line file1.
  • S3 is start line file2.
  • S4 is end line file2.

Skipping line 1-4:

diff <(sed -n '5,10p' file1) <(sed -n '5,10p' file2)

$ more file1
1
2
3
4
5
6
7
8
9
10

$ more file2
11
2
3
4
5
65
7
8
9
10

Result

2c2
< 6
---
> 65

-> it skipped the diff on line 1.

It might be easier to use a desktop tool for this. Meld is excellent ( topic on AU: https://askubuntu.com/a/2947/15811 ):

Meld is a visual diff and merge tool targeted at developers. Meld helps you compare files, directories, and version controlled projects. It provides two- and three-way comparison of both files and directories, and has support for many popular version control systems.

Rinzwind
  • 299,756