I have two nearly identical files, "foo" and "bar".
Each file has thousands of lines, one number each line.
If I sort
and then diff
both files, this is the result:
$ diff foo bar
5984a5985,5986
> 15676
> 15677
7703,7706d7704
< 17486
< 17487
< 17488
< 17489
However, if I grep -Fvf
both files, I get zero "non-matches" in both directions:
$ grep -Fvf foo bar
$ grep -Fvf bar foo
$
Why is that? Shouldn't I be getting results similar to diff? I'd expect the first command to return me the "first half" of the values on diff, and the second grep command to return me the rest.
Why am I not getting the expected results?
Best Answer
For
diff
, unlikegrep -Fvf
, the order that lines appear matters. Also, since the-x
option was not included in the grep command, lines with mere partial matches are excluded from the output.Reproducible example
Consider these two files:
The
diff
command shows they are different while thegrep
commands produce no output:If we were to add
-x
to require full-line matches, then grep produces:Additional suggestion
As abligh suggests in the comments,
diff -u
produces more context which can make the output easier to understand.