# Emails in CSV B that are not in CSV A

I have two CSV lists of email addresses (List A and B). Our marketing guys accidentally sent an email to people in List A instead of combining both lists before sending.

An email needs to be sent to people in List B that were not in List A.

How can I find out which email is in list B that's not in list A? I wrote a PHP script but for educational and when the marketing guys do the same accident again I would like know if there's linux commands to do this instead of running a PHP script. (I know I could just run the script but I'm sure there's a better way).

I found the following solution but it combines the lists rather than showing which email is in List B and not in List A: How to join two CSV files?

-

You could try

cat {A,A,B}.csv | sort | uniq -u


cat prints out contents of both files, sort sorts the output (obviously) and uniq -u deletes all the duplicate entries (leaving only the ones with one occurence) if you have a double entry in one of the lists it will get deleted of course, so you have to ensure before, that you don't. As pointed out by Ben Voigt in the comments you have to cat A twice to ensure deletion of every entry in it.

You could append >> C.csv to the command line to write the content to a file.

cat {A,A,B}.csv | sort | uniq -u  >> C.csv

-
Woah! Thanks :) I only know a couple of linux tricks. I'll try this... If it actually works it's way simpler than my 75 lines PHP script... If anyone else has a different solution please feel free to add it as an answer. It will help me a lot with learning linux tricks. –  iDev247 Jun 19 '12 at 19:37
Shouldn't it be cat {A,A,B}.csv | sort | uniq -u to guarantee that all entries from A.csv get removed? –  Ben Voigt Jun 19 '12 at 20:20
true, thanks for the hint –  Baarn Jun 19 '12 at 20:33

You could import into Excel/OpenOffice etc.

• Combine the two lists into one. (paste both lists in a single column)

• Select all addresses in this one column.

• Use the conditional formatting to, say, highlight only the unique values.

• sort by that conditional formatting.

-