1

I am trying to go through individual emails and retrieve the host name. Each email has a To: section with an email address "abc123@aol.com". I'm trying to retrieve just "aol.com"

Eg:

To: abc123@aol.com (abc123)

To: jim@yahoo.com,hk (Jim)

To: Jim@yahoo.com\ (Jim)

Expected output:

aol.com

yahoo.com,hk

yahoo.com\
0

Try:

sed 's/.*@\(.*\) .*/\1/g' input

Or:

awk -F '[@ ]' '{print $3}' input

This one keeps a final space:

grep -Eo '@.* ' input
0

I would use GNU grep's perl-compatible regexes:

grep -oP '(?<=@)\S+'
0

You can keep it simple and use cut:

cut -d'@' -f2

For example, if you had these emails in one big file called emails.txt:

grep 'To:' emails.txt | cut -d'@' -f2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.