1

Consider I have two text files.

First File name - "Emails.txt" with the following data:

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

Second text file - "Banned.txt" with the following strings:

@gotmail.com
@cmail.com
@uor.edu

How to delete all the lines in the 1st text file "Emails.txt" if it matches the stings of any line present in the second text file "Banned.txt"?

The desired output of the new file should be:

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

Can this be done using SED or awk in Linux? Can you please suggest how to do this?

1 Answer 1

1

grep -v is enough. The flag -f allows you to do exactly what you want:

grep -vf Banned.txt Emails.txt

If you want to do something more complicated out of the list of banned addresses, e.g. impose that they match the whole of the domain, you'll need to generate a regex from your Banned file:

cat Banned.txt | tr "\n" "|" | sed -e 's,|,$\\|,g' | sed -e 's,\\|$,,'

gives the desired

@gotmail.com$\|@cmail.com$\|@uor.edu$

Then:

cat Banned.txt | tr "\n" "|" | sed -e 's,|,$\\\\|,g' | sed -e 's,\\|$,,' | xargs -i grep -v '{}' Emails.txt

(doubling the number of escapes \ as they're being evaluated when going through xargs). This will match and remove [email protected] but not e.g. [email protected].

5
  • This is not correct. The Output of the file should show ONLY the line whose sting doesn't match with Banned.txt file. Which means, all the domains / strings listed in the Banned.txt file should get deleted in the first file Emails.txt Commented Jan 23, 2019 at 12:46
  • That's the purpose of the -v. Have you tried it?
    – Joce
    Commented Jan 23, 2019 at 12:48
  • 1
    grep -vf worked like a charm... Thanks. Commented Jan 23, 2019 at 12:58
  • 1
    After some more research, I found - Rather using a long regex, this works with this simple line too: grep -vf <(sed 's/$/$/' Banned.txt) Emails.txt Commented Jan 28, 2019 at 12:04
  • 1
    @JoneyWalker Indeed that's shorter!
    – Joce
    Commented Jan 28, 2019 at 15:27

You must log in to answer this question.

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