Is this correct?
grep -Eiorh '([[:alnum:]_.-]+@[[:alnum:]_-]+?\.[[:alpha:].]{2,6})' "$@" | sort | uniq
I can't tell. I don't know much about UNIX either. Sorry!
Here's an improved version of the previous script that's more robust and removes duplicates. To sort emails, however, use Excel or Numbers or please ask someone else to help you.
Admittedly, AppleScript is not the best way to go if you have hundreds of mails to handle.
global leftDelimiters, rightDelimiters
set whiteSpaceConstants to space & tab & return & linefeed ¬
& characterid 8232 & characterid 160
set leftDelimiters to whiteSpaceConstants & "<([:;"
set rightDelimiters to whiteSpaceConstants & ">)]:;,?!"
-- modify the above delimiters if necessary
set theEmailAddresses to {} -- a list
set theRef to a reference to theEmailAddresses
tell application "Mail"
repeat with thisMessage in (get selection)
try
copy (reply to of thisMessage) to the end of theRef
repeat with thisAddress in my getAddresses(content of thisMessage)
copy contents of thisAddress to the end of theRef
end repeat
end try
end repeat
end tell
-- Remove duplicates :
set theShorterList to {}
repeat with thisAddress in theRef
set thisAddress to contents of thisAddress
if thisAddress is not in (theShorterList as text) then
copy thisAddress to the end of theShorterList
end if
end repeat
-- Convert the list to text (as a list of paragraphs) :
set TID to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set theText to (text items of theShorterList) as text
set AppleScript's text item delimiters to TID
-- Display the result in a new TextEdit document :
tell application "TextEdit"
makenewdocumentat front with properties {text:theText}
activate
end tell
on getAddresses(theText)
set theAddresses to {}
repeat
set j to offset of "@" in theText
if j = 0 then exit repeat -- no more email addresses
set thisAddress to "@"
set L to length of theText
set i to j
repeat
set i to i - 1
if i = 0 then exit repeat
if (text item i of theText) is in leftDelimiters then exit repeat
end repeat
set k to j
if i + 1 < j then
repeat
set k to k + 1
if k > L then exit repeat
if (text item k of theText) is in rightDelimiters then exit repeat
end repeat
if k - 1 > j then
set thisAddress to text (i + 1) through (k - 1) of theText
if last character of thisAddress is "." then
set thisAddress to text 1 through -2 of thisAddress
end if
copy thisAddress to the end of theAddresses
end if
end if
if (k ≥ L) or (j = L) then exit repeat
set theText to text (j + 1) through -1 of theText
end repeat
return theAddresses
end getAddresses
That's the best I can do. Hope it can help.
Message was edited by: Pierre L. (replaced “repeat with thisAddress in theEmailAddresses” with “repeat with thisAddress in theRef”