Diff and "--GTYPE-group-format" Diff and "--GTYPE-group-format" unix unix

Diff and "--GTYPE-group-format"


It's sort of the way diff groups things. In your changed group, you're printing the new change and not the old, which would be

U 23

So the groupings that diff sees is:

`1 -> 1` unchanged`2,3 -> 2a` changed`4,5 -> 4,5` unchanged`  -> 6` new

In order for diff to group a match as "old", there has to be an unchanged before and after. So if file2 was like this:

1a2456

And you ran the same diff command, you'd get this:

U 1aD 3I 6

Because there is a 2 -> 2 and 4 -> 4 that is unchanged so the missing 3 gets grouped as "old".


To complement Jon Lin's helpful answer:

While you can't use the --<gtype>-group-format options directly to always show deleted lines (as explained in Jon's answer), the --side-by-side (-y) output-format option does contain the desired information, and you can use awk to reformat it as desired:

diff --suppress-common-lines --side-by-side file1 file2 |  awk -F'\t+' '$2 ~ / +\|/ { print "U " $3 }              $2 ~ / +</  { print "D " $1 }              $2 ~ / +>/  { print "I " $3 }'

With your sample files this yields:

U 2aD 3I 6

which the above awk command produced from the following --side-by-side output:

2                                 | 2a3                                 <                                  > 6