sorting multiple columns in different order

sorting multiple columns in different order

Post by John » Sun, 24 Nov 2002 09:54:08



I'm trying to sort a space deliminated file with six columns.  I'd
like to sort columns 2 and 4 in ascending order then sort column 5
where columns 2and4 are uniq in decending numeric order.

Following command seems to work on solaris 2.6 but not on 2.8.

cat file|sort -k 2,4 -k 5nr

any ideas?

 
 
 

sorting multiple columns in different order

Post by John » Sun, 24 Nov 2002 12:09:33



> I'm trying to sort a space deliminated file with six columns.  I'd
> like to sort columns 2 and 4 in ascending order then sort column 5
> where columns 2and4 are uniq in decending numeric order.

> Following command seems to work on solaris 2.6 but not on 2.8.

> cat file|sort -k 2,4 -k 5nr

> any ideas?

It is not clear what you want, in particular what do
"sort columns 2 and 4 " and
"where columns 2and4 are uniq" mean?

Some examples would be good.

But, from what you say, you do not mean "-k 2,4" as
this takes in column 3 which you do not want.
Try: sort -k2,2 -k4,4 -k5,5nr file (you do not need cat).

Now, why should it change from Solaris 2.6 to 8?
A couple of possibilities spring to mind.
1. different locales (LC_COLLATE).
2. sort is not stable -- there are no guarantees about
    what happens to items that compare equal.
3. you are being misled because your arguments to
    sort do not mean what you think they do.

John.

 
 
 

1. Sorting By Second Column With Unique First Column

I need to sort data by the second column while keeping
the first column unique. There is probably an easy way to
do this that I'm totally missing.
    The first column is an inmate number. The second column
is the bond amount. I'm going to output the criminals based
on the bond amount, with the highest bonds at the top.
    My problem is that if someone has two bond amounts, that
person makes the list twice. I only one folks listed based on
the highest individual bond amount. (A guy with a $10k bond
should be before someone with three $5k bonds.)

Unsorted                          What I Want
98051461~250.00                   98051385~5000.00
98051461~0.00                     98051487~750.00
98051403~500.00                   98051403~500.00
98051487~750.00                   98051461~250.00
98051487~750.00
98051385~1500.00
98051385~1500.00
98051385~0.00
98051385~0.00
98051385~5000.00

    I'm using... sort -rnt "~" -k 2 $INPUT | uniq ...which
solves the problem when someone has multiple bonds of the
same amount but doesn't help when the bonds are varying.
    A perl solution would be a wonderful thing but I'm currently
getting the data externally (open DINNER, "sort -rnt \"~\" -k 2
$Temp | uniq |" or die "Can't fork: $!" ;) so I'll take any
solution that works. (Yes, the above is cheezy.)
    All suggestions (and requisite mocking) welcome.
    Many thanks.

    Matt

2. NCR UNIX on FreeBSD

3. how do I sort a file based on multiple columns

4. mouse for X11

5. How to have sort start sorting froma given column?

6. In a script, how to update specific rows in an Informix table?

7. SORT command > sorting decimal numbers in columns on a file not working !

8. #pragma pack

9. Multiple SCSI cards, force different scanning order?

10. "sort" sorting uppercase/lowercase differently on different boxes

11. How to obtain the result of the order find on several columns

12. sort sort: 0653-657 A write error occurred while sorting (4.1.3)

13. sort by columns