How to ignore some differences in diff command?

Vahagn picture Vahagn · Dec 14, 2010 · Viewed 28.7k times · Source

diff has an option -I regexp, which ignores changes that just insert or delete lines that match the given regexp. I need an analogue of this for the case, when changes are between two lines (rather then 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 has not such kind of ability. Is there any suitable alternative for diff?

Answer

John Kugelman picture John Kugelman · Dec 14, 2010

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)