home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / mail / misc / 2484 < prev    next >
Encoding:
Internet Message Format  |  1992-07-27  |  1.8 KB

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