Attn: sed(1) regular expression gurus
Steve Coile
scoile at nandomedia.com
Tue Jul 15 04:24:42 PDT 2003
On Mon, 14 Jul 2003, D J Hawkey Jr wrote:
>I'm getting really frustrated by a seemingly simple problem. I'm doing
>this under FreeBSD 4.5.
>
>Given these portions of an e-mail's multi-line Received header as tests:
>
> by some.host.at.a.com (Postfix) with ESMTP id 3A4E07B03
> by some.host.at.a.com (8.11.6) ESMTP;
> by some.host.at.a.different.com (8.11.6p2/8.11.6) ESMTP;
> by some.host.at.another.com ([123.4.56.789]) id 3A4E07B03
> by some.host.at.yet.another.com (123.4.56.789) id 3A4E07B03
# tested with sed-4.0.5-1 for RHL 9.0
# remove junk we don't care about
s/^.*by \([^ ]*\) (\([^)]*\)).*$/\1 \2/
# identify valid hostname
s/^[[:alnum:]][-[:alnum:]]*\(\.[[:alnum:]][-[:alnum:]]*\)*/host:&/
# identify valid IP address (w/o brackets)
s/[[:digit:]]\{1,3\}\(\.[[:digit:]]\{1,3\}*\)\{3\}$/ipaddr:&/
# identify valid IP address (w/brackets)
s/\[\([[:digit:]]\{1,3\}\(\.[[:digit:]]\{1,3\}*\)\{3\}\)\]$/ipaddr:\1/
# discard if no valid hostname or IP address
/\(^host:\| ipaddr:\)/!d
# if valid IP address, discard anything else
s/^.* ipaddr://
# if valid hostname, discard anything else
s/^host:\([^ ]*\).*$/\1/
--
Steve Coile
Systems Administrator
Nando Media
ph: 919-861-1200
fax: 919-861-1300
e-mail: sysadmins at nandomedia.com
http://www.nandomedia.com
More information about the freebsd-questions
mailing list