How can I ignore some differences in the 'diff' command?
Asked Answered
P

5

46

diff has an option, -I regexp, which ignores changes that just insert or delete lines that match the given regular expression. I need an analogue of this for the case, when changes are between two lines (rather than insert or delete lines).

For instance, I want to ignore all differences like between "abXd" and "abYd", for given X and Y.

It seems diff doesn't have any such kind of ability. Is there a suitable alternative for diff?

Pickens answered 13/12, 2010 at 23:1 Comment(0)
S
34

You could filter the two files through sed to eliminate the lines you don't care about. The general pattern is /regex1/,/regex2/ d to delete anything between lines matching two regexes. For example:

diff <(sed '/abXd/,/abYd/d' file1) <(sed '/abXd/,/abYd/d' file2)
Surovy answered 13/12, 2010 at 23:6 Comment(3)
Thanks for the answer. sed '/regex/d' file deletes all lines in file where a matching of regex occurs. Is there a way to delete not the line but only the matching part of it?Pickens
I'm not sure exactly what you want to do. Can you edit your question with an example of two files you want to diff and what you want the result to be?Surovy
Unfortunately, this will not work with recursive (-r) diff for obvious reasons.Phyl
H
31

Improving upon the earlier solution by John Kugelman:

diff <(sed 's/ab[XY]d/abd/g' file1) <(sed 's/ab[XY]d/abd/g' file2)

is probably what you may be looking for! This version normalizes the specific change on each line without deleting the line itself. This allows diff to show any other differences that remain on the line.

Harriette answered 27/4, 2012 at 5:18 Comment(0)
C
3

You could use sed to replace instances of the pattern with a standard string:

diff <(sed 's/ab[XY]d/ab__REPLACED__d/g' file1) <(sed 's/ab[XY]d/ab__REPLACED__d/g' file2)
Cotenant answered 21/2, 2014 at 12:10 Comment(0)
D
2

Assuming X and Y are single characters, then -I 'ab[XY]d' works fine for me.

Doubtful answered 13/12, 2010 at 23:6 Comment(1)
This entirely ignores the line. If there are other differences in the line that you do care about, this will hide them.Fireball
V
2

My open source Linux tool 'dif' compares files while ignoring various differences.

It has many options for doing search/replace, ignoring whitespace, comments, or timestamps, sorting the input files, ignoring certain lines, etc.

After preprocessing the input files, it runs the Linux tools Meld, gvimdiff, tkdiff, diff, or Kompare on these intermediate files.

Installation is not required. Just download and run the 'dif' executable from dif.

For your use case, try the search and replace option:

./dif file1 file2 -search 'ab[XY]d' -replace 'abd' -diff
Varipapa answered 14/9, 2020 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.