Reputation: 17087
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:
One that will return a list that excludes any email address that matches ANY ONE of the domains in the second list (I will consider those ones the "good" ones)
One that will return a list with ONLY the email addresses that match ANY ONE of the domains in the second list (I will delete users from my site who belong to those addresses)
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
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
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