简体   繁体   中英

Diff and “--GTYPE-group-format”

I have two files I want to compare with diff . Changed lines should get a prefix "U", new lines "I" and deleted ones "D":

file1:

1
2
3
4
5

file2:

1
2a
4
5
6

diff --old-group-format="D %<" \
--new-group-format="I %>" \
--changed-group-format="U %>" \
--unchanged-group-format="" file1 file2

The output is:

U 2a
I 6

But where is

D 3

?

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 2
3

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:

1a
2
4
5
6

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

U 1a
D 3
I 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 2a
D 3
I 6

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

2                                 | 2a
3                                 <
                                  > 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM