How to ignore some differences in diff command?

前端 未结 5 823
情书的邮戳
情书的邮戳 2021-02-06 23:13

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 ca

相关标签:
5条回答
  • 2021-02-06 23:26

    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)
    
    0 讨论(0)
  • 2021-02-06 23: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.

    0 讨论(0)
  • 2021-02-06 23:31

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

    0 讨论(0)
  • 2021-02-06 23:34

    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)
    
    0 讨论(0)
  • 2021-02-06 23:36

    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 https://github.com/koknat/dif

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

    ./dif file1 file2 -search 'ab[XY]d' -replace 'abd' -diff
    
    0 讨论(0)
提交回复
热议问题