Best way to remove lines from a file where matching text (not whole line) exists in another file

text processing

I have a file of email addresses (file 1)
and another file (file 2) of lines of data that contain some of these email addresses

I want to compare files and remove all lines in file 2 that have a matching email in file 1.

I know I can do a loop and use sed -i
or loop and grep each line to a new file then compare files with COMM

But I was wondering if there is a comparison method that I can kind of grep all lines in file1 to file2 and just be left with the lines from file2 that do not contain any emails in file 1

file 1:

test@invalid.com
test2@invalid.com
test3@invalid.com

file 2:

23456|tom|jones|test@goodemailcom|10
12345|pete|best|pete@goodemail.com|10
87569|remove3|me3|test3@invalid.com|10
23098|mike|jones|mike@goodemailcom|10
10985|al|best|al@goodemail.com|10
09865|removve|me|test@invalid.com|10
13579|remove2|me2|test2@invalid.com|10

Appreciate any knowledge anyone has.

Best Answer

You can use fgrep for this:

fgrep -v -f file1  file2  > unique_addresses

This task will be a lot easier if you have 1 email address per line in both files.

Traditionally fgrep exists as a separate program, but in GNU utilities, grep -F does the same thing.

Related Question