How to remove the lines which appear on file B from another file A?

slhck picture slhck · Dec 6, 2010 · Viewed 93.3k times · Source

I have a large file A (consisting of emails), one line for each mail. I also have another file B that contains another set of mails.

Which command would I use to remove all the addresses that appear in file B from the file A.

So, if file A contained:

A
B
C

and file B contained:

B    
D
E

Then file A should be left with:

A
C

Now I know this is a question that might have been asked more often, but I only found one command online that gave me an error with a bad delimiter.

Any help would be much appreciated! Somebody will surely come up with a clever one-liner, but I'm not the shell expert.

Answer

The Archetypal Paul picture The Archetypal Paul · Dec 6, 2010

If the files are sorted (they are in your example):

comm -23 file1 file2

-23 suppresses the lines that are in both files, or only in file 2. If the files are not sorted, pipe them through sort first...

See the man page here