3

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.

4

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.