home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / question / 9383 < prev    next >
Encoding:
Text File  |  1992-07-24  |  2.9 KB  |  88 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!wupost!cs.utexas.edu!hermes.chpc.utexas.edu!news.utdallas.edu!convex!tchrist
  3. From: Tom Christiansen <tchrist@convex.COM>
  4. Subject: Re: Sorting articles of variable number of lines
  5. Message-ID: <1992Jul24.135204.12136@news.eng.convex.com>
  6. Originator: tchrist@pixel.convex.com
  7. Keywords: Sorting text Files
  8. Sender: usenet@news.eng.convex.com (news access account)
  9. Nntp-Posting-Host: pixel.convex.com
  10. Reply-To: tchrist@convex.COM (Tom Christiansen)
  11. Organization: CONVEX Realtime Development, Colorado Springs, CO
  12. References: <4346@disuns2.epfl.ch>
  13. Date: Fri, 24 Jul 1992 13:52:04 GMT
  14. X-Disclaimer: This message was written by a user at CONVEX Computer
  15.               Corp. The opinions expressed are those of the user and
  16.               not necessarily those of CONVEX.
  17. Lines: 69
  18.  
  19. From the keyboard of ebel@lsesun3.epfl.ch (Norbert EBEL):
  20. :Hi Netters,
  21. :
  22. :If I want to sort text files containing articles of variable length
  23. :according to a fixed criteria I thought of the following solution:
  24. :
  25. :1) make a very long line with an article putting a special sequence in 
  26. :   order to remember the end of line, using tr, awk or sed
  27. :2) sorting according the given criteria using sort
  28. :3) rechanging the special sequence to nl
  29. :A problem is most utilities allows for limmited length of line
  30. :
  31. :Is there any better solution or possibly a utility for that.
  32. :
  33. :I found this problem when I saved mails in the wrong incoming order
  34. :in a folder. For that special pourpose there is special option on Sun in 
  35. :the mail utility but I can imagine other situations where a similar 
  36. :problem will occur.
  37. :
  38. :Please answer by e-mail. Thanks
  39.  
  40. Well, I did: when I read the question in another newsgroup.
  41. Apparently, the poster posted the same message to multiple
  42. groups without using the Newsgroups line.  Please do this.
  43. Somewhere Jonathan Kamens has a stock chastisement for this.
  44.  
  45. My answer was brief:
  46. This is much easier to do in perl.
  47. Read each message into an array element,
  48.  
  49.     $* = 1;  # make ^ match at beginning of line, not just of string
  50.     $i = -1;
  51.     while (<>) {
  52.         /^From / && $i++;
  53.         $arts[$i] .= $_;
  54.     } 
  55.  
  56. Now do the sort:
  57.  
  58.     @newarts = sort my_routine @arts;
  59.  
  60.     sub my_routine {
  61.         # two msgs are in $a and $b; return 0, 1, or -1 per qsort
  62.     } 
  63.  
  64. If it's really short, just inline the thing
  65.  
  66.     require 'getdate.pl';  # the yacc-to-perl version, of course 
  67.  
  68.     print sort {
  69.     local($d1) = $a =~ /^Date:\s+(.*)/;
  70.     local($d2) = $b =~ /^Date:\s+(.*)/;
  71.     &getdate($d1) <=> &getdate($d2)
  72.     };
  73.  
  74. Now, this is actually pretty inefficient.  It would be better
  75. to do this:
  76.  
  77.     grep(push(@idx, &getdate(/^From\s+\S+\s+(.*)/)), @arts);
  78.     print @arts[sort { $idx[$a] <=> $idx[$b] } 0..$#idx];
  79.  
  80. But that's a bit subintuitive.
  81.  
  82. --tom
  83. -- 
  84.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  85.  
  86.  
  87.     "We don't care.  We don't have to.  We're the Phone Company."
  88.