LQ Newbie
Registered: Sep 2004
Posts: 11
Rep:
|
Unix data sort question
Using the shell's sort command, I can sort a data file, even specificy what columns to sort, and if the data should be sorted as alphanumeric, or numeric.
My problem is when I want to sort a file on multiple columns, where some of those columns needs to be sorted as alpha, and other columns as numeric.
Sort doesn't seem to handle this... it can only treat data as alpha or numeric, AFAIK.
How, using native shell tools (can expand into sed, awk, if necessary) can I get my data sorted as I want?
As an example I would like:
Z,hello world1,101
AZ,hello world1,100
AZ,hello world1,102
AA,hello world1,50
AA,hello world1,6
AZ,hello world1,30
to be sorted as
AA,hello world1,6
AA,hello world1,50
AZ,hello world1,30
AZ,hello world1,100
AZ,hello world1,102
Z,hello world1,101
TODAY, using sort
ALPHA (default) example
sort -t, -k 1,1 -k 3,3 filename >filename.srt results in:
AA,hello world1,50
AA,hello world1,6
AZ,hello world1,100
AZ,hello world1,102
AZ,hello world1,30
Z,hello world1,101
Numeric (-n) example
sort -n -t, -k 1,1 -k 3,3 filename >filename.srt results in:
AA,hello world1,6
AZ,hello world1,30
AA,hello world1,50
AZ,hello world1,100
Z,hello world1,101
AZ,hello world1,102
Last edited by amytys; 06-30-2005 at 10:34 AM.
|