How to get the difference (only additions) between two files in linux How to get the difference (only additions) between two files in linux bash bash

How to get the difference (only additions) between two files in linux


All of the below is copied directly from @TomOnTime's serverfault answer here:

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.sortedsort <b >b.sortedcomm -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)


diff and then grep for the edit type you want.

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


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       [...]