Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to get (GNU)DIFF to printout only lines that are different in one file. So given

    ==> diffa.txt <==
    line2 - in a only
    line4 changed

    ==> diffb.txt <==
    line4 changed in b
    line6 in b only

i would like diff --someoption diffa.txt diffb.txt to produce

    line2 - in a only

    line4 changed

The following looks as though it should be helpful but it is a bit cryptic :

          Similar, but format GTYPE input groups with GFMT.

          Similar, but format all input lines with LFMT.

          Similar, but format LTYPE input lines with LFMT.

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

          GFMT may contain:

   %<     lines from FILE1

   %>     lines from FILE2
share|improve this question
The man page is definitely a bit terse on these flags! Good question. – quickshiftin Oct 18 '13 at 0:01
I would like to point out that ' info diff ' will give complete examples, there is a wealth of information in info. – Baroudi Safwene Jul 6 at 17:24
up vote 28 down vote accepted

Not sure diff alone can do it but you can always use the power of other GNU utilities to help you.

diff -u diffa.txt diffb.txt | grep '^-[^-]' | sed 's/^-//'

It does the diff, then selects only the lines that begins with '-' - those are changed and have values from diffa.txt file, then sed just remove those '-' signs.

Edit: After few experiments with diff, looks like the below command produces what you want:

diff --changed-group-format='%<' --unchanged-group-format='' diffa.txt diffb.txt
share|improve this answer
For a one off the pipe method is fine. I would prefer ` perl -ne"print if s/^-//" ` but that is taste. I think I see what --changed-group-format='%<' are doing for me now.... – justintime Apr 27 '10 at 14:08
Thanks. :D I needed something like this to list all my packages between two computers and I used sdiff but this looks a bit better. – Rob Sep 21 '11 at 14:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .