116

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.

1
  • are all added lines in A2 new for the file? I mean no duplicates with existing lines?
    – Kent
    Mar 13, 2013 at 12:10

8 Answers 8

124

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)
6
  • 5
    Since we are talking about bash here the last command can be simplified to comm -12 <(sort < a) <(sort < b) using process substitution.
    – phk
    Feb 24, 2017 at 12:13
  • 2
    You sir, are my hero. Feb 4, 2019 at 16:49
  • Why does an answer that works only on sorted files have so many upvotes, despite the question clearly stating that the two files are unsorted?
    – Fritz
    Nov 27, 2021 at 11:51
  • This answer contains the command (see the last one in particular) that works if the files are not sorted. I think it makes sense to give the unsorted commands first since they are easier to understand, and to build up to the commands that do not assume sorted.
    – scottkosty
    Dec 6, 2021 at 19:47
  • @scottkosty: Thanks for your response. Sorry, I didn't communicate my point clearly in my previous comment. My issue with this approach (in contrast to the diff-based answers) is that the order of the lines is jumbled up by sort, and in many cases, order matters. Imagine comparing two source code files to see which function was added. Only, you sort the source code alphabetically before... Sorting code does not make sense. This is why I think the other answers are better. I think you should at least mention this caveat somewhere in your answer.
    – Fritz
    Dec 8, 2021 at 17:06
81

diff and then grep for the edit type you want.

diff -u A1 A2 | grep -E "^\+"
5
  • 5
    This will leave you with + at the beginning of the line
    – kgadek
    Aug 8, 2016 at 13:18
  • 10
    You can remove those with sed: diff -u A1 A2 | grep '^\+' | sed -E 's/^\+//' Nov 1, 2018 at 23:07
  • @AmauryD your edit gets rid of the first +++ A2 line, but leave a + sign at the beginning of every line, which is what the comment and sed command above are about.
    – remram
    Jun 10, 2020 at 21:54
  • 3
    You can combine the grep and sed in one command: diff -u A1 A2 | sed -n '/^+[^+]/ s/^+//p'
    – remram
    Jun 10, 2020 at 21:55
  • Drawback: this solution leaves lines which reference line numbers, e.g. @@ -31,6 +630,8 @@ Jul 6, 2020 at 18:56
71

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

       [...]
6
  • 5
    can you please explain these options, i couldnt get them from the man page Mar 13, 2013 at 12:29
  • 3
    See this link for more gnu line group formats
    – Premjith
    Mar 13, 2013 at 12:39
  • 1
    The '' after --unchanged-group-format='' looks like a single ", which won't work. Maybe change the '' to "" lest someone types your answer in with a single ". Mar 29, 2018 at 15:24
  • 7
    This is a much better answer than the selected answer, btw. Gives you exactly what you want, rather than the output littered with + symbols and an unnecessary meta line. Mar 29, 2018 at 15:25
  • 1
    This is the best answer,
    – Annahri
    May 29, 2020 at 15:22
13

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
8

You can type:

grep -v -f A1 A2
3
  • Assume file A1 contains one line x, and file A2 contains one line x and the other line xx. This command outputs nothing since both lines in A2 contains x.
    – timrau
    Mar 13, 2013 at 12:16
  • 2
    grep's -x (--line-regexp) can be used to ensure the entire line is matched. So if A1 contains x and A2 contains xx, a match will not be found. Oct 1, 2018 at 16:44
  • 3
    You probably also need to use the option -F or --fixed-strings. Otherwise grep will be interpreting A1 as regular expressions. So if A1 contains the line .*, it will match everything. So the entire command would be: grep -vxF -f A1 A2
    – wisbucky
    Aug 28, 2019 at 20:55
7
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.

1
  • 1
    Best answer ! This return exactly the lines that have been added and nothing more. This should be the accepted answer I think Jun 16, 2017 at 13:24
7

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.