How to remove matching lines from one text file using another

text processing

I am trying to remove matching lines from one text file using another. For example, fired.txt contains:

Jason
Candice
Brent
Tom

And I want to remove matching lines from workers.txt, which currently looks like this:

Andrew
Tommy
Peter
Jason
Brent
Sasha
Tom
Candice

So, in the end, it would remove the entire line if it matches exactly, so it would look like this:

Andrew
Tommy
Peter
Sasha

I am using macOS Sierra, btw.

Best Answer

Short grep approach:

grep -xvf fired.txt workers.txt

The output:

Andrew
Tommy
Peter
Sasha

grep options:

  • x - Select only those matches that exactly match the whole line

  • v - Invert the sense of matching, to select non-matching lines

  • f (--file=file) - Obtain patterns from file, one per line

Related Question