如何得到差异(只有添加)在 linux 的两个文件

我有两个文件 A1和 A2(未排序)。A1是 A2的前一个版本,一些行已经添加到 A2。如何获得添加到 A2的新行?

注意: 我只是想添加新的行,并不希望行在 A1,但删除在 A2。当我做 diff A1 A2,我得到的添加和删除,但我只想添加。

请提出一个方法来做这件事。

147315 次浏览

diff and then grep for the edit type you want.

diff -u A1 A2 | grep -E "^\+"

The simple method is to use :

sdiff A1 A2

Another method is to use comm, as you can see in Comparing two unsorted lists in linux, listing the unique in the second file

You can type:

grep -v -f A1 A2

You can try this

diff --changed-group-format='%>' --unchanged-group-format='' A1 A2

The options are documented in man diff:

       --GTYPE-group-format=GFMT
format GTYPE input groups with GFMT

and:

       LTYPE is 'old', 'new', or 'unchanged'.
GTYPE is LTYPE or 'changed'.

and:

              GFMT (only) may contain:


%<     lines from FILE1


%>     lines from FILE2


[...]

Most of the below is copied directly from @TomOnTime's serverfault answer here. At the bottom is an attempt that works on unsorted files, but the command sorts the files before giving the diff so in many cases it will not be what is desired. For well-formatted diffs of unsorted files, you might find the other answers more useful (thanks to @Fritz for pointing this out):

Show lines that only exist in file a: (i.e. what was deleted from a)

comm -23 a b

Show lines that only exist in file b: (i.e. what was added to b)

comm -13 a b

Show lines that only exist in one file or the other: (but not both)

comm -3 a b | sed 's/^\t//'

(Warning: If file a has lines that start with TAB, it (the first TAB) will be removed from the output.)

NOTE: Both files need to be sorted for "comm" to work properly. If they aren't already sorted, you should sort them:

sort <a >a.sorted
sort <b >b.sorted
comm -12 a.sorted b.sorted

If the files are extremely long, this may be quite a burden as it requires an extra copy and therefore twice as much disk space.

Edit: note that the command can be written more concisely using process substitution (thanks to @phk for the comment):

comm -12 <(sort < a) <(sort < b)
git diff path/file.css | grep -E "^\+" | grep -v '+++ b/' | cut -c 2-
  • grep -E "^\+" is from previous accepted answer, it is incomplete because leaves non-source stuff
  • grep -v '+++ b' removes non-source line with file name of later version
  • cut -c 2- removes column of + signs, also may use sed 's/^\+//'

comm or sdiff were not an option because of git.

A similar approach to https://stackoverflow.com/a/15385080/337172 but hopefully more understandable and easy to tweak:

diff \
--new-line-format="%L" \
--old-line-format="" \
--unchanged-line-format="" \
A1 A2