Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!husc6!seismo!rochester!pt.cs.cmu.edu!cadre!pitt!gecrd1!maciolek From: maciolek@gecrd1.UUCP Newsgroups: comp.unix.questions Subject: Re: sort question... Message-ID: <116@gecrd1.UUCP> Date: Fri, 15-May-87 15:36:22 EDT Article-I.D.: gecrd1.116 Posted: Fri May 15 15:36:22 1987 Date-Received: Sun, 17-May-87 00:52:03 EDT References: <303@nugipsy.UUCP> <651@custom.UUCP> Reply-To: maciolek@gecrd1.UUCP (Mike Maciolek) Distribution: na Organization: GE Res & Dev Center, Schenectady, NY Lines: 103 Summary: Long! and boring to anyone not interested in sorting In article <651@custom.UUCP> boykin@custom.UUCP (Joseph Boykin) writes: >In article <303@nugipsy.UUCP>, tcianflo@nugipsy.UUCP (Tom Cianflone) writes: >> How would I set up the file so that I could get the sort >> utility to: >> 1) keep family members together without mixing up names >> with other families of the same last name >> 2) force the listing of a family group to be in a specified >> order, for example, head-of-house followed by children. > >You would, obviously, have to place some other information in the >file. However, sort does allow you to specify multiple sort fields. >It will sort using the first key, then sort lines which >are common by the subsequent sort keys. That is, you could have a >file which has: > Last First X > >where 'X' is A for head of household, B for spouse, C for kids, >(D for pets!), etc. To sort by family, and have it sorted by >"rank" within family, issue the following command: > > sort +0 -1 +2 -3 filename > >This can be expanded to provide the information you were looking >for such as keeping people within the same family together, etc. Well, this won't work either. You'll wind up with something like Smith Alice A Smith Bob A Smith Carol A Smith Doris B Smith Ted C Smith Bill C and won't know whether Ted Smith is a child of Alice, Bob or Carol. To make sure that families get grouped according to head-of-household, you will have to be a little fancier. Here are a couple of ideas... First - all family names are prefaced by the head-of-household for that family. This is quite redundant and space-consuming, but it gets the job done...unless two families have heads-of-household with the same name. Thus, a file could be entered : Smith Alice Smith Alice C Smith Bill Smith Bob Smith Bob B Smith Doris Smith Bob C Smith Ted Smith Carol As I said, this makes for a lot of redundancy, especially in families with many spouses :-) or children, since all family-members who ar not heads-of-household are prefixed by the name of their head-of-household. Another solution that seems more feasible would be to group all family members onto the same LINE as the head-of-household. This is significant, of course, because sort(1) has this notion of "lines" which are delimited by newline characters. Everything between a pair of newlines is treated as a unit. SO...all you have to do is write a little filter which reads a file like this: Smith Bob A Smith Doris B Smith Ted C Smith Carol A Smith Alice A Smith Bill C and produces a file like this: Smith Bob A ^MSmith Doris B ^MSmith Ted C Smith Carol A Smith Alice A ^MSmith Bill C by joining all the lines which follow an 'A' (head-of-household) line UNTIL another head-of-household is encountered. I use control-M as a separator here, though any character which would not appear in the text would be fine. Now, when you run this file through sort(1), the output will be sorted by head-of-household, with identically-named heads of household sorted next by spouse, then by child. Take the output and run it through an inverse of your original filter which converts ^M's back to newlines: filter1 outfile An advantage is that you don't have to look up the syntax for specifying the key field positions to sort(1). The caveat here is that in the initial file, the head of household always has to precede spouse and child entries, AND the spouse and child entries will not be sorted under head-of-household. Having now beaten this subject to death, I hope I don't see umpty-zillion followups by nit-pickers. And thank you for your support. -- Mike Maciolek seismo!rochester!pt.cs.cmu.edu!cadre!pitt!gecrd1!maciolek -consulting for- General Electric "Epoxy can be cured."