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 have two plain text files, each file is containing a list of strings sorted alphabetically with one string per line. I want to diff the files and have an output of all strings that exist only in file2.

Preferabbly I want the operation to be possible without any 3rd party tools, or with a minimum of installations of tools that is "normal" to find in a windows command line environment, such as GNU Diffutils, Powershell, etc. The output should be in text form (file or as command line output).

Example:

File 1 contents:
A
C
D

File 2 contents:
A
B
C
E

Result wanted:
B
E
share|improve this question
up vote 2 down vote accepted
comm -13 file_a file_b

(-13 is -1 -3)


diff a b | grep "^>"
diff a b | sed -n "/^>/ s/^> //p"

You can get a very minimal toolset from UnxUtils -- outdated, yes, but enough for this one.

share|improve this answer
    
Works great! Thanks! – PHeiberg Jun 11 '10 at 14:32

In Windows PowerShell (direct port of grawity's diff/grep combo):

Compare-Object (Get-Content file1) (Get-Content file2) |
    Where-Object { $_.SideIndicator -eq '=>' } |
    Select-Object -ExpandProperty InputObject

This can be shortened to:

diff (gc file1) (gc file2) | ?{$_.SideIndicator -eq '=>'} | %{$_.InputObject}

or wrapped in a function if you need it more often than once.

share|improve this answer
    
+1 Too bad I can't accept two answers. Works like a charm. Thanks! – PHeiberg Jun 11 '10 at 14:32

You must log in to answer this question.

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