6

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.

1 Answer 1

11

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

1
  • 2
    If the F (--fixed-strings) option is added, the match will be literal instead of interpreted as a regular expression. This is useful if the strings to match contain brackets, braces, parenthesis or other special regex character.
    – eocanha
    Commented Apr 8, 2021 at 11:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .