george
george

Reputation: 462

How to remove lines contained in file 1 from file 2 if in file 2 they are prefixed?

I have the following situation:

source.txt

ID1:[email protected]
ID2:[email protected]
ID3:[email protected]
...

IDs are numeric strings, e.g. 1234, 23412, 897... (one or more digits).

exclude.txt

[email protected]
[email protected]
[email protected]
...

i.e. only emails, no IDs.

I want to remove all lines from source.txt which contain emails listed in exclude.txt, preserving the ID:email pairs for the lines which are not removed.

How can I do that with linux command line tools (or simple bash script if needed)?

Upvotes: 2

Views: 51

Answers (1)

George Vasiliou
George Vasiliou

Reputation: 6335

You can do it easily with awk:

awk -F":" 'NR==FNR{a[$1];next}(!($2 in a))' exclude.txt source.txt

Alternative with grep:

grep -v -F -f exclude.txt source.txt

Use grep with care, since grep does a regex matching. You might need to add also -w option to grep (word matching)

Upvotes: 2

Related Questions