home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!wupost!cs.utexas.edu!hermes.chpc.utexas.edu!news.utdallas.edu!convex!tchrist
- From: Tom Christiansen <tchrist@convex.COM>
- Subject: Re: Sorting articles of variable number of lines
- Message-ID: <1992Jul24.135204.12136@news.eng.convex.com>
- Originator: tchrist@pixel.convex.com
- Keywords: Sorting text Files
- Sender: usenet@news.eng.convex.com (news access account)
- Nntp-Posting-Host: pixel.convex.com
- Reply-To: tchrist@convex.COM (Tom Christiansen)
- Organization: CONVEX Realtime Development, Colorado Springs, CO
- References: <4346@disuns2.epfl.ch>
- Date: Fri, 24 Jul 1992 13:52:04 GMT
- X-Disclaimer: This message was written by a user at CONVEX Computer
- Corp. The opinions expressed are those of the user and
- not necessarily those of CONVEX.
- Lines: 69
-
- From the keyboard of ebel@lsesun3.epfl.ch (Norbert EBEL):
- :Hi Netters,
- :
- :If I want to sort text files containing articles of variable length
- :according to a fixed criteria I thought of the following solution:
- :
- :1) make a very long line with an article putting a special sequence in
- : order to remember the end of line, using tr, awk or sed
- :2) sorting according the given criteria using sort
- :3) rechanging the special sequence to nl
- :A problem is most utilities allows for limmited length of line
- :
- :Is there any better solution or possibly a utility for that.
- :
- :I found this problem when I saved mails in the wrong incoming order
- :in a folder. For that special pourpose there is special option on Sun in
- :the mail utility but I can imagine other situations where a similar
- :problem will occur.
- :
- :Please answer by e-mail. Thanks
-
- Well, I did: when I read the question in another newsgroup.
- Apparently, the poster posted the same message to multiple
- groups without using the Newsgroups line. Please do this.
- Somewhere Jonathan Kamens has a stock chastisement for this.
-
- My answer was brief:
- This is much easier to do in perl.
- Read each message into an array element,
-
- $* = 1; # make ^ match at beginning of line, not just of string
- $i = -1;
- while (<>) {
- /^From / && $i++;
- $arts[$i] .= $_;
- }
-
- Now do the sort:
-
- @newarts = sort my_routine @arts;
-
- sub my_routine {
- # two msgs are in $a and $b; return 0, 1, or -1 per qsort
- }
-
- If it's really short, just inline the thing
-
- require 'getdate.pl'; # the yacc-to-perl version, of course
-
- print sort {
- local($d1) = $a =~ /^Date:\s+(.*)/;
- local($d2) = $b =~ /^Date:\s+(.*)/;
- &getdate($d1) <=> &getdate($d2)
- };
-
- Now, this is actually pretty inefficient. It would be better
- to do this:
-
- grep(push(@idx, &getdate(/^From\s+\S+\s+(.*)/)), @arts);
- print @arts[sort { $idx[$a] <=> $idx[$b] } 0..$#idx];
-
- But that's a bit subintuitive.
-
- --tom
- --
- Tom Christiansen tchrist@convex.com convex!tchrist
-
-
- "We don't care. We don't have to. We're the Phone Company."
-