Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore some differences in diff command?

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?

like image 411
Vahagn Avatar asked Dec 13 '10 23:12

Vahagn


People also ask

What is the use of diff command?

diff is a command-line utility that allows you to compare two files line by line. It can also compare the contents of directories. The diff command is most commonly used to create a patch containing the differences between one or more files that can be applied using the patch command.


2 Answers

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) 
like image 88
John Kugelman Avatar answered Sep 20 '22 05:09

John Kugelman


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.

like image 20
Josy P. Pullockara Avatar answered Sep 23 '22 05:09

Josy P. Pullockara