Remove all invalid emails, unsubscribed emails or bounced emails from Email List

This post describes how you can use a shell script for removed mistyped or invalid emails from the email list which you have downloaded from your email subscription provider. For example, if a user types his email with invalid characters like space, / , & etc then such emails are not valid and we have to remove such email ids from our list or also duplicated emails to avoid sending multiple emails to same user. This saves our cost to send emails to our subscribers and also to avoid sending emails to bounced emails.

We assume, we have following 3 files,
1. all-exported-emails.txt => this file contains list of all the emails as people have entered in subscribe form.
2. unsubscribed.txt => contains list of emails of people unsubscribed
3. bounced.txt => contains list of emails which we have already tried previously sending and have bounced.

 $ vim 
sort -u all-exported-emails.txt > unique.txt
#bash unique.txt
rm -rf final.txt
while read line
#remove unwanted character from email
if [ "$NEW_MAIL" == "${NEW_MAIL//[\,\' ]/}" ]
        check=$(grep -r $NEW_MAIL unsubscribed.txt)
        if [ "$check" =  "$NEW_MAIL" ]; then
                echo "email $NEW_MAIL found in unsubscribed.txt"
                check2=$(grep -r $NEW_MAIL bounced.txt)
                if [ "$check" =  "$NEW_MAIL" ]; then
                        echo "email $NEW_MAIL found in bounced.txt"
                        echo $NEW_MAIL >> final.txt
   echo "email $NEW_MAIL contains space, comma or quote.. hence ignoring"
done < unique.txt
 $ bash 
Follow Lynxbee

Leave a Reply / Ask Question