Can awk do multi-key sorting? 
Author Message
 Can awk do multi-key sorting?

Is it possible to do multi-key sorting with awk?
I've got to sort several hundred fixed-length
records before I can process them. Each record
is 700 characters long and I need to sort as follows:

substr($0, 568, 20)    #last name
substr($0, 1, 4)       #integer with leading zeros
substr($0, 20, 2)      #integer with (possibly) leading zero

There are no field separators, by the way. Can someone give
me a jump start on this? Indeed, is awk the right tool?
(If possible, would you also copy responses to my email,
since I can't get on the 'Net as often as I want.) Thanks
for any help you can give.

Mario

* Sent from RemarQ http://www.*-*-*.com/ The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!



Sat, 25 May 2002 03:00:00 GMT  
 Can awk do multi-key sorting?

Quote:

> Is it possible to do multi-key sorting with awk?
> I've got to sort several hundred fixed-length
> records before I can process them. Each record
> is 700 characters long and I need to sort as follows:

> substr($0, 568, 20)    #last name
> substr($0, 1, 4)       #integer with leading zeros
> substr($0, 20, 2)      #integer with (possibly) leading zero

> There are no field separators, by the way. Can someone give
> me a jump start on this? Indeed, is awk the right tool?

Awk is not the right tool.  You should use sort, assuming you're on a UNIX
system or have access to a UNIX-style sort for whatever system you are on.
Here's the syntax you'd use with sort:

    sort -k 0.568,0.587 -k 0.1,0.4n -k0.20,0.21n

Sort keys are specified starting with the major key followed by the minor
keys.  It was ambiguous what field you actually intend as the major key but
I assume it's the last name.  If not, just reverse the order of the key
specifications.

Nicki



Sun, 26 May 2002 03:00:00 GMT  
 Can awk do multi-key sorting?


Quote:
> Is it possible to do multi-key sorting with awk?
> I've got to sort several hundred fixed-length
> records before I can process them. Each record
> is 700 characters long and I need to sort as follows:

> substr($0, 568, 20)    #last name
> substr($0, 1, 4)       #integer with leading zeros
> substr($0, 20, 2)      #integer with (possibly) leading zero

> There are no field separators, by the way. Can someone give
> me a jump start on this? Indeed, is awk the right tool?
> (If possible, would you also copy responses to my email,
> since I can't get on the 'Net as often as I want.) Thanks
> for any help you can give.

Like Nicole Hamiltone says sort is your tool for this kind of task. But
in case you're not on a Unix box or you need to sort the file as an
integrated part of a bigger task Perl might be the a better tool you
need. It can do whatever sorts you fancy.
/Peter
--
-= Spam safe(?) e-mail address: pez68 at netscape.net =-

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 27 May 2002 03:00:00 GMT  
 Can awk do multi-key sorting?

Quote:

> Like Nicole Hamiltone says sort is your tool for this kind of task. But
> in case you're not on a Unix box or you need to sort the file as an
> integrated part of a bigger task Perl might be the a better tool you
> need. It can do whatever sorts you fancy.

If he's not on a Unix box, he will have much less trouble installing GNU
sort, it has been ported to many platforms.

Regards...
                Michael



Tue, 28 May 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Display the Sort Field First on a Multi-Key Browse

2. sorting multi-keyed arrays ( tktable problem )

3. Binary Sort/Merge Sort in awk

4. Auto Inc Key with Must be in List Field in Multi Part Key

5. ok, so how do I return a sorted list without doing it in place

6. I need help doing a prolog multi-insert!!!

7. Can this be done in AWK?

8. How is this done in awk/nawk/gawk?

9. Can this be done with AWK or ED ?

10. Done with range limit over three component key!!! :-)

11. Sorting Multi-selection enhanced list views

12. multi criterions sorting(2)

 

 
Powered by phpBB® Forum Software