Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

bash

diff

I have two files A1 and A2 (unsorted). A1 is previous version of A2 and some lines have been added to A2. How can I get the new lines that are added to A2?

Note: I just want the new lines added and dont want the lines which were in A1 but deleted in A2. When i do diff A1 A2, I get the additions as well as deletions but I want only additions.

Please suggest a way to do this.

like image 328
user1004985 Avatar asked Mar 13 '13 12:03

user1004985


People also ask

How can I compare two files for differences?

Right-click on the first file. Click on “Select for Compare” from the menu. Proceed to right-click on the second file. Click on “Compare with Selected.

Which command gives all differences between two files?

diff stands for difference. This command is used to display the differences in the files by comparing the files line by line.

How do I compare two files side by side in Linux?

sdiff command in linux is used to compare two files and then writes the results to standard output in a side-by-side format. It displays each line of the two files with a series of spaces between them if the lines are identical.


1 Answers

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) 
like image 115
scottkosty Avatar answered Nov 10 '22 09:11

scottkosty