Sorting text tabular type data
Alan Jackson
ajackson
Mon May 17 11:37:07 PDT 2004
On Mon, 02 Sep 2002 15:58:12 -0700 (PDT)
"stayler" <stayler at xmtservices.net> wrote:
> Here is a small example of one of the files.
>
> 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
>
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.
Here are some quick examples ...
I'd start by putting in the new delimiter by using sed, since that
second field has those ugly embedded blanks.
cat inputfile | sed 's/ */,/' | sed 's/ *NV */,NV,/' > delimitedfile
^^ note 2 blanks
Let's count the number of distinct things based on the second field sorted
from most to least...
#!/usr/bin/perl -w
use strict;
my %hash;
while (<>){
chomp;
my @line = split(',');
$hash{$line[1]}++;
}
foreach (sort {$hash{$b}<=>$hash{$a}} keys %hash) {
print "$_ : $hash{$_}\n";
}
make a pretty list sorted by the first field...
#!/usr/bin/perl -w
use strict;
my @data = <>;
# now use the Schwartzian transform to sort it and print it
print map {$_->[1]}
sort {$a->[0] cmp $b->[0]}
map {[(split(','))[0], sprintf("%-8s %-30s %2s %-10s",split(','))]}
@data;
--
-----------------------------------------------------------------------
| Alan K. Jackson | To see a World in a Grain of Sand |
| alan at ajackson.org | And a Heaven in a Wild Flower, |
| www.ajackson.org | Hold Infinity in the palm of your hand |
| Houston, Texas | And Eternity in an hour. - Blake |
-----------------------------------------------------------------------
More information about the Linux-users
mailing list