How to ignore some differences in diff command?

瘦欲@ 提交于 2019-12-04 15:13:05

问题


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?


回答1:


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)



回答2:


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.




回答3:


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




回答4:


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)


来源:https://stackoverflow.com/questions/4434346/how-to-ignore-some-differences-in-diff-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!