Search This Blog

Saturday, March 28, 2009

quick script to extract email ids from a detailed mail header

Since my creative juices are flowing so much today, I thought of posting the simple, yet effective awk script to extract sender's email ids from a detailed mail message (even though if the mail id be ecrypted).

A word of caution is that the file being processed should have a line with "EOF" value (without strings) ONLY at the end of file, otherwise the awk script will hang.

BEGIN { print FILENAME | "wc -l |cut -f1 -d' '" ; }
/^X-Sender:/ || /^From / { #print NR, $0;
#
# get the real userid from where the email came
#
m=split($0,b,"@");
from=b[1];
#print from;
x=split(from,c," ");
realfrom=c[x];
gsub("<","",realfrom);
#print realfrom;
#
# get the domain name of the smtp server now
#
while ($0 !~ /HELO/ && $0 != "EOF") getline;
if ($0 == "EOF") exit;
domain=$5; gsub(")","",domain); n=split(domain,a,".")
#print n, domain;
realdomain=a[n-1]"."a[n];
if (n>1) print realfrom"@"realdomain;
next;
}

No comments:

Post a Comment