#!/bin/env python
# This script can be used to extract each email address present in a log
# and print its one per line
# Do you like it? follow me on telegram: http://t.me/linuxcheatsheet
# Author: Gianluca Mascolo
# Mailto: gianluca _at_ gurutech _dot_ it
# Requirement: https://github.com/JoshData/python-email-validator
# Download this script locally on your computer
# and install python-email-validator to run it
import sys
import re
from email_validator import validate_email, EmailNotValidError
# I will read standard input line by line
# use a regex to find something that is similar to an email address
# and validate it using email validator
# if ok print it, otherwise go ahead
for line in sys.stdin:
EmailList = re.findall(r"\b[a-zA-Z0-9+!#$%&'=?_.^~-]+@[a-zA-Z0-9_+.-]+\b", line)
for Email in EmailList:
try:
v = validate_email(Email,check_deliverability=False)
print (v['email'])
except EmailNotValidError:
pass