Sorting text tabular type data

stayler stayler
Mon May 17 11:37:08 PDT 2004


On Mon, 2 Sep 2002 21:12:28 -0500, Alan Jackson wrote:

>Years ago I would have used awk, or a chain of sed, sort, awk, whatever.
>But now I do almost all that stuff in perl.

Thats what I've been doing up til now.

I got it sorted by the 3rd field, but I need to sort that set by the
last field in decending order.  This perl approach seems interesting as
awk a bit cumbersome.  

Here is what I've gotten so far

this is the command line I use:

sed -f cleanup.sed june.results | sectionsort

here is the contents of cleanup.sed:

s/ \{1,30\}/ /g
s/ALL /ALL-/g
s/SINGLE-OP ALL-LOW/SINGLE-OP-L/g
s/ALL-HIGH//g
s/ALL-High//g
s/ALL-LOW//g
s/  / /1
s/ /,/g

and this is sectionsort:

#! /bin/sh
awk -F, '{
	print $3 ", " $0
	}' $* | \
sort | \
awk -F, ' {
if ($1 == LastSection) print "\t" $2, $3, $4, $5;
		else { LastSection = $1
		print "\n" $1, "\t" $2, $3, $4, $5
		}
}'

Here is a small sample of the test data, I am testing this on a much
larger file that I don't want to bother the list with:

K7ICW         SINGLE-OP ALL HIGH           NV    13125    
K7XC          MULTI-LIMITED                NV    1856     
KC6UCN        MULTI-LIMITED                NV    8477     
N6MI          ROVER                        NV    3968     
N7LQ          MULTI-UNLIMITED ALL HIGH     NV    82128    
N7ROJ         ROVER                        NV    7722     
NW7O          ROVER                        NV    2847     
W7PW/R        ROVER                        NV    12       
WB6YIY        SINGLE-OP ALL LOW            NV    1020     

I just need to sort by the last numerical value within the section
sorted blocks, the 3rd string.

Your perl approach is interesting but I've gotten so close using awk
and I am learning a great deal.  I am worried that I may of coded
myself into a corner as this last sort is proving to be rather
daunting.  But I may yet beat this.

suggestions are always welcome, I may even take some of your perl
suggestion......  hrm

stayler



More information about the Linux-users mailing list