Merc
Merc

Reputation: 17087

Trim, from a list of email addresses, all addresses matching a "forbidden domain" list

I have a list of email addresses (in a text file, one address per line):

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

I also have a list of domains (in a text file, one domain per line):

d1.com
d2.com

I am trying to write two bash scripts:

What's the best, easiest way to get this done? I am rusty with bash, and am finding it tricky. The regular expression is basic.

Please note that I am not after complete solutions, but "key commands" to make this happen.

Upvotes: 0

Views: 137

Answers (2)

pasaba por aqui
pasaba por aqui

Reputation: 3539

Use grep command, like:

grep -f allowed_domains emails

to get the allowed emails, where "allowed_domains" is the second file you show in the question, "emails" is the first one. . Add "-v" for the not allowed emails.

If you want something stronger, add a "@" at start of each allowed_domain line. By example, as:

cat allowed_domains | xargs -L1 printf "@%s\n" | grep -f - emails

Upvotes: 1

anubhava
anubhava

Reputation: 786101

You can use this awk command:

awk -F@ 'NR==FNR{dom[$0]; next} {print > (($2 in dom)? "bad.txt":"good.txt")}' file2 file1

cat good.txt
[email protected]
[email protected]
[email protected]

cat bad.txt
[email protected]
[email protected]
[email protected]
[email protected]

Upvotes: 0

Related Questions