Re: sort by 2nd and 3rd fields
From: Dale Hagglund (dale.hagglund_at_gmail.com)
Date: 06/11/05
- Previous message: John L: "Re: sort by 2nd and 3rd fields"
- In reply to: Harry: "Re: sort by 2nd and 3rd fields"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 11 Jun 2005 08:21:50 GMT
Harry> But the folllowing will screw up the 2nd (major sort) column.
Harry> sort -t- -n -k2,2 -k3,3
The -n option applies to all the fields, so the second field gets
sorted numerically as well as the third field. Unfortunately, since
none of the values in field 2 are numeric, all the fields compare as
equal.
You can attach a flag to each field selector as follows:
$ sort -t- -k2,2 -k3,3n
The trailing "n" on on the final argument applies numeric sorting to
just the third field of the data. On your original sample input, this
command gives:
TYPE1-Apple-1
TYPE1-Apple-2
TYPE1-Apple-7a
TYPE1-Apple-8
TYPE2-Apple-10
TYPE2-Apple-12
TYPE1-Apple-13
TYPE2-Apple-14
TYPE2-Apple-15
TYPE2-Apple-17
TYPE1-Apple-19
TYPE1-Orange-29
TYPE1-Orange-35
TYPE1-Orange-38
TYPE1-Orange-45
I don't think it will always do the right thing, though, with fields
like "7a" and so on because they are not strictly numeric. You'll
have to experiment with your dataset to make sure things work as
desired.
Dale.
- Previous message: John L: "Re: sort by 2nd and 3rd fields"
- In reply to: Harry: "Re: sort by 2nd and 3rd fields"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|