sorting on one column

Hello all,

Is there a way to sort only one column while keeping everything else intact. Take for example this situation: (all columns are space separated)

11 AA asdf 1 -0.5 xx
11 AA axdf 1 -0.6 xx
11 AA csls 1 -0.7 xx
11 AA hjkj 1 -0.4 xx
11 AA uius 1 -0.8 xx
22 AA asdf 1 -0.4 xx
22 AA axdf 1 -0.7 xx
22 AA csls 1 -0.6 xx
22 AA hjkj 1 -0.9 xx
22 AA uius 1 -0.2 xx

the output should have the 1st, 2nd, 3rd, 4th and 6th columns intact and only the 5th column needs to be sorted. i.e. note the 5th column has negative values.

11 AA asdf 1 -0.4 xx
11 AA axdf 1 -0.5 xx
11 AA csls 1 -0.6 xx
11 AA hjkj 1 -0.7 xx
11 AA uius 1 -0.8 xx
22 AA asdf 1 -0.2 xx
22 AA axdf 1 -0.4 xx
22 AA csls 1 -0.6 xx
22 AA hjkj 1 -0.7 xx
22 AA uius 1 -0.9 xx

Please help with this. Thanks.

Yes. Look for -k option - Man Page for sort (OpenSolaris Section 1) - The UNIX and Linux Forums

thanks. i know i can use -k5,5nr but then it will not consider the change in ids in the column 1.

Sorry. You can do this with awk. I give you a solution during 10 minutes.

in your input:

11 AA hjkj 1 -0.4 xx

in our output:

11 AA asdf 1 -0.4 xx

What's that meaning?

Maybe you can try this:

sort -k1n -k5rn -k2 -k3 -k4 -k6 infile
#!/bin/ksh 
awk '{print $5}' /tmp/yourinputfile|sort -nr > /tmp/temp
paste /tmp/yourinputfile /tmp/temp|awk '{ print $1 " " $2 " " $3 " " $4 " "$7 " " $6}'

Ok. With awk you should use a temp file (or it would be too complex), IMO. Anyway dude2cool wrote the programm according your description (with a little bug - there are only 6 fields).

I've written a simple program on perl but then I see that your example of the output is more complex than your description.

You want to sort records (lines with the same ID) according column 5 and you don't want touch columns 2,3,4, and 6 (the first one should be the same for all the record).

Am I right?

Hi,

Here a perl solution:

$ cat script.pl
use warnings;
use strict;

## Accept one argument.
@ARGV == 1 or die qq(Usage: perl $0 input-file\n);

## Open input file or return with error.
open my $fh, "<", $ARGV[0] or die qq(Cannot open input file $ARGV[0]: $!\n);

## Get the column of numbers of the input file and sort it descending.
my @file = <$fh>;
my @numbers = sort { $b <=> $a } map { (split)[4] } @file;

## Reopen input file to process it from the beginning.
close $fh;
open $fh, "<", $ARGV[0] or die qq(Cannot open input file $ARGV[0]: $!\n);
while ( <$fh> ) {
        my @f = split;
        $f[4] = shift @numbers;
        printf "%s\n", "@f";
}
close $fh;
$ cat infile
11 AA asdf 1 -0.5 xx
11 AA axdf 1 -0.6 xx
11 AA csls 1 -0.7 xx
11 AA hjkj 1 -0.4 xx
11 AA uius 1 -0.8 xx
22 AA asdf 1 -0.4 xx
22 AA axdf 1 -0.7 xx
22 AA csls 1 -0.6 xx
22 AA hjkj 1 -0.9 xx
22 AA uius 1 -0.2 xx
$ perl script.pl infile
11 AA asdf 1 -0.2 xx
11 AA axdf 1 -0.4 xx
11 AA csls 1 -0.4 xx
11 AA hjkj 1 -0.5 xx
11 AA uius 1 -0.6 xx
22 AA asdf 1 -0.6 xx
22 AA axdf 1 -0.7 xx
22 AA csls 1 -0.7 xx
22 AA hjkj 1 -0.8 xx
22 AA uius 1 -0.9 xx

Regards,
Birei