[geeks] awk q: sorting on two different fields

William Kirkland bill.kirkland at gmail.com
Sun Jul 30 10:02:37 CDT 2006


Sorry about my previous reply, I did not get the subject correct, and  
it therefor split ...

Note: the following example does not actually sort *in* awk! AND  
would be much
easier to maintain with a wrapper script passing the data already  
sorted to the
awk script to be formatted. There are a number of other limitations  
as well.

#! /usr/bin/awk -f
BEGIN {
     for( i=1; i<ARGC; i++ ) {
         list=sprintf( "%s %s", list, ARGV[i] )
     }
     system( "sort -t ',' -k 2 -k 1 " list )
}
FILENAME != "" { next }
{ print }

On Jul 29, 2006, at 23:45, geeks-request at sunhelp.org wrote:

> Message: 4
> Date: Sat, 29 Jul 2006 19:58:01 -0400
> From: Patrick Giagnocavo <patrick at zill.net>
> Subject: [geeks] awk q:  sorting on two different fields
> To: geeks at sunhelp.org
> Message-ID: <1154217481.376149.34.camel at unknown>
> Content-Type: text/plain; charset=ASCII
>
> The combined brainpower of this list will no doubt make short work on
> this awk question ....
>
> I am doing a directory for a non-profit organization.  I am able to
> pre-process the CSV-like file and output to Postscript.
>
> However, what is really desired is for the data to be sorted on two
> different fields, alpha by $city and then alpha by $orgname in that
> city.
>
> e.g. given a hypothetical data set like
>
> Founder's Harvest, Philadelphia
> Lancaster Market, Lancaster
> John's Market, Lancaster
> Vento's Organic Cheese Steaks, Philadelphia
>
> I would end up with :
> (Lancaster sorts before Philadelphia)
> (Founder's appears alpha sorted before Vento's)
>
> John's Market, Lancaster
> Lancaster Market, Lancaster
> Founder's Harvest, Philadelphia
> Vento's Organic Cheese Steaks, Philadelphia
>
> Any idea how to do this in awk?
>
> I already have their directory broken out by state, just need to sort
> within the state by city and organization name.
>
> --Patrick



More information about the geeks mailing list