Selective diff in bash

Viewed 172

I want to compare two files with diff, but I want to skip some lines. The lines that I want to skip are marked as comments, but only in the first file.

For easy copy/paste testing, I write the diff command with process substitution:

$ diff -y <(echo -e "aaa\nbbb\n#ccc\nddd") <(echo -e "AAAAAA\nbbb\nccc\nddd")
aaa                           | AAAAAA
bbb                             bbb
#ccc                          | ccc
ddd                             ddd
$

As can be seen both first line and third line is marked as a diff( | ). I do not want third line in the diff because of the # sign. I can ignore matching lines with the -I option, but that requires that both files match the pattern.

$ diff -y -I "#.*" <(echo -e "aaa\nbbb\n#ccc\nddd\n#eee") <(echo -e "AAAAAA\nbbb\nccc\nddd\n#EEEE")
aaa                           | AAAAAA
bbb                             bbb
#ccc                          | ccc
ddd                             ddd
#eee                            #EEEE
$ 

Is there a way to ignore lines based on only the first file?

Edit: I forgot to write that I need the return value(echo $?) of diff to tell me if there are any non-matching lines (excluding the ones that I do not want to check).

2 Answers
Related