Jed's Journal

Yes. I'm blogging straight from a terminal.

Extract email addresses between angle brackets using sed

May 09, 2017 — Jed Mallen

If you have a list of email addresses from outlook with proper names and emails in angle brackets (<>) then you can pipe that file into this script and come out with a long line of emails separated by a comma.


# Usage: cat emaillist.txt | emailtrim.sh
# emaillist.txt contains something similar to the one below:
# John Doe <johndoe@email.com>; Jane Doe <jane.doe@email.com>; hax0r <black@hat.net>

emaillist=$1

sed 's/; /\n/g' $emaillist | sed 's/^.* <\(.*\)>/\1/g' | sed '/^$/d' | sed ':a;N;$!ba;s/\n/,/g'

# or just with a list of email addresses separated by newlines if you omit the last pipe:
# sed 's/; /\n/g' $emaillist | sed 's/^.* <\(.*\)>/\1/g' | sed '/^$/d'

# eof

Tags: sed, email