LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Unix data sort question (https://www.linuxquestions.org/questions/programming-9/unix-data-sort-question-338745/)

amytys 06-30-2005 10:31 AM

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

zeropash 06-30-2005 11:43 AM

sort -t, -k1,1d -k3,3n filename

bigearsbilly 07-01-2005 05:24 AM

read the 'man' page ;-)


All times are GMT -5. The time now is 03:11 AM.