home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!usc!nic!netlabs!lwall
- From: lwall@netlabs.com (Larry Wall)
- Newsgroups: comp.mail.misc
- Subject: Re: extract adding address
- Message-ID: <1992Jul27.164239.17483@netlabs.com>
- Date: 27 Jul 92 16:42:39 GMT
- References: <1992Jul24.184037.15528@infonode.ingr.com>
- Sender: news@netlabs.com
- Organization: NetLabs, Inc.
- Lines: 33
- Nntp-Posting-Host: scalpel.netlabs.com
-
- In article <1992Jul24.184037.15528@infonode.ingr.com> fordke@infonode.ingr.com (Keith Ford) writes:
- :
- : I would like to pipe a RFC 822 message into a program and have
- : it return the return address. The Reply-To: line should take
- : precedence (sp?) over the From: line. I'm using a UNIX box
- : with System V. A shell script or C program would be nice.
- : I don't use perl, but I guess I could learn.
-
- Here's the usual Perl incantation for such things. It relies on two
- subtle tricks: first, if you split a string using a pattern with
- parens, it returns the corresponding portions of the delimiters as well
- as the fields between delimiters, and second, if you assign a list to
- an associative array, each pair of strings becomes one key-value pair.
- Once you've done that you can look at the entries in any order you please.
-
- #!/usr/bin/perl
- $/ = ""; # paragraph mode
- $* = 1; # multi-line pattern matching
- $_ = <>; # read one paragraph
- %head = ('PRESTUFF', split(/^(\S+):\s*/)); # split on entry names
- print $head{'Reply-To'} || $head{'From'}; # print the one you want
-
- You can even get at the uucp-style colon-less From line if you so desire
- since that will be part of PRESTUFF, if the article contains such a line.
-
- Also, if you're allergic to answers with embedded newlines, you can say
-
- s/\n[ \t]+/ /g; # join multi-line entries
-
- just before the split line.
-
- Larry Wall
- lwall@netlabs.com
-