[geeks] awk q: sorting on two different fields

William Kirkland bill.kirkland at gmail.com
Mon Jul 31 16:28:22 CDT 2006


Damm, I had thought I had gotten lucky ... this is lame, in that I  
forgot
to add the most important part back in ...

Note: the following example code suffers from a number of limitations  
and
still does not perform the sort "in" awk, it forks another process which
sort the data and then passes that data to another instance of the awk
script.

The 'BEGIN' extracts the list of arguments (file names only, if your  
script
requires additional arguments, additional processing will be needed to
implement), generates a list then forks another process calling itself,
explicitly by name (/tmp/sort.awk).
The 'FILENAME' check is used to limit the recursion to one level (no
processing of the data is performed on a pass that also provides a
filename).
The final line, is used to demonstrate that the data is accessible
for typical awk processing.

#! /usr/bin/awk -f
BEGIN {
     for( i=1; i<ARGC; i++ ) {
         list=sprintf( "%s %s", list, ARGV[i] )
     }
     if ( ARGC > 1 )
     system( "sort -t ',' -k 2 -k 1 " list " | /tmp/sort.awk" )
}
FILENAME != "" { next }
{ printf( "%s: %s\n", FILENAME, $0 ) }

On Jul 31, 2006, at 08:14, geeks-request at sunhelp.org wrote:

> #! /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 }



More information about the geeks mailing list