Hi.
I often find it cumbersome to craft a specialized sort routine with the builtin perl sort. There is a module that, while not trivial, will make a sorting function for you. Here's an example of that, driven by a shell script:
#!/usr/bin/env bash
# @(#) s4 Demonstrate short, flexible perl code.
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for i;do printf "%s" "$i";done; printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && . $C perl
FILE=${1-data1}
pl " perl code:"
cat p1
pl " Input file $FILE:"
cat $FILE
pl "Results:"
./p1 $FILE
exit 0
producing:
% ./s4
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0.8 (lenny)
GNU bash 3.2.39
perl 5.10.0
-----
perl code:
#!/usr/bin/env perl
# @(#) p1 Demonstrate sorting rows, function from Sort::Maker.
# As noted in "Perl Best Practices", p 164 ff.
# http://search.cpan.org/~uri/Sort-Maker-0.06/Sort/Maker.pm
use Sort::Maker;
# Set key as string beginning with second character.
my $sorter = make_sorter( 'ST', string => '/.(\w+)/' );
die "make_sorter: $@" unless $sorter;
# Omit but save fields 1 & 2, key is string beginning with fields 3-.
while (<>) {
( $t1, $t2, @a ) = split /\s+/;
@b = $sorter->(@a);
print join( " ", $t1, $t2, @b, "\n" );
}
exit(0);
-----
Input file data1:
1 a1 ,AB ,AC ;AD ,EE
2 a2 ,WE ;TR ,YT ,WW
3 a3 ;AS ,UY ;RF ,YT
-----
Results:
1 a1 ,AB ,AC ;AD ,EE
2 a2 ;TR ,WE ,WW ,YT
3 a3 ;AS ;RF ,UY ,YT
Best wishes ... cheers, drl