Reputation: 2803
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.
Upvotes: 122
Views: 177641
Reputation: 11
You can filter only the lines that need to be added to fileA
to become equal to fileB
.
We include the first line that indicates the line number to apply the change.
diff $fileA $fileB | grep '^>' -B 1
Upvotes: 1
Reputation: 2600
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)
Upvotes: 132
Reputation: 6055
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
Upvotes: 15
Reputation: 3147
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
Upvotes: 7
Reputation: 1198
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
[...]
Upvotes: 77
Reputation: 171
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 stuffgrep -v '+++ b'
removes non-source line with file name of later versioncut -c 2-
removes column of +
signs, also may use sed 's/^\+//'
comm
or sdiff
were not an option because of git.
Upvotes: 8
Reputation: 23058
diff
and then grep
for the edit type you want.
diff -u A1 A2 | grep -E "^\+"
Upvotes: 87