Article 74878 of alt.folklore.computers: From: hoey@aic.nrl.navy.mil (Dan Hoey) Newsgroups: alt.folklore.computers Subject: Re: Behavior of sed (was: Re: Interviewing) Date: 23 Jan 1995 21:50:39 GMT Organization: Navy Center for Artificial Intelligence Distribution: usa In-reply-to: justin@physics20's message of 23 Jan 1995 19:06:34 GMT Peter Seebach sed: > >s/\([^ ]*\) \([^ ]*\)/\2 \1/g > >'a b c d e' should produce? Cute puzzle. justin@physics20 (Justin R. Bendich) bleated: > Why do you make such demands on poor sed? SunOS and VMS (probably > has same source code) seds produce "b ac d e", which is sed's way of > crying out for mommy (without the trailing 'g', the behavior is > correct). IRIX's sed yields "b a d c e". I guess that's "right". No, the former answer is right. Once you notice that in the second substitution \1 is "" and \2 is "d" it is easy to see why the correct result is "b ac d e " (not "b ac d e"). I think I have heard that some seds refuse to match "[^ ]*" to a null string within s/.../.../g for fear of an infinite loop. I would expect that detecting the infinite loop would be so trivial that even a writer of U*x system software would be able to prevent its occurrence without mangling the semantics in an obscure and undocumented way. IRIX's behavior shows that my expectation is too generous. Though I don't think this is SGI's fault--I suspect they are following some brain-dead standard designed to preserve the historic lossage, like maggots in amber. > It's hard to see why you'd want to do this... Haven't you been paying attention? We are doing this to figure out who we want to hire. Would you hire someone who couldn't figure out that sed was doing the right, if nonobvious, thing? How would she ever debug her sed scripts? Now if you want an _obscure_ sed hack, analyze: #! /bin/sh echo "$*" | sed \ 's/./&&/g;s/\(.\).*\(.\)/\2&\1/;s/.\(.\)/\1)(&/g;s/..//;s/...$// s/(\(.\)\1\1)/-/g;s/(\(.\)\1.)/]/g;s/(.\(.\)\1)/[/g;s/(...)/./g;h;G;P;D' \ | more I'm told that a patent for the above process was applied for, and that the application reads in part: Fantomark strings which, by definition, are imperceptible to man, animal, or machine, nonetheless have well-defined streaks, which are ordinary mark strings.... If one assigns NOUGHT to quids and BEING to quods, and if one calls BIP processing (which amounts to continual liquidating and quodizing) by the term BECOMING; and calls liquidating DISSOLUTION and quodizing PASSAGE INTO, then the intrinsic opposition between quidness and quodness and its continual resolution through BIP processing is captured by the language: BECOMING continually mediates the DISSOLUTION of BEING into NOUGHT and the PASSAGE of NOUGHT INTO BEING. I offer 57 fantomarks for the cleverest alternative vocabulary substitions; payment in zorkmids or qatloos is subject to varying exchange rates. Dan Hoey@AIC.NRL.Navy.Mil