LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sorting text file on different columns in different orders (https://www.linuxquestions.org/questions/linux-newbie-8/sorting-text-file-on-different-columns-in-different-orders-763005/)

arshadul 10-19-2009 01:53 PM

sorting text file on different columns in different orders
 
I'm trying to sort a text file with 10 columns. The columns are separated by comma. The sorting is to be done by three columns, and the order needs to be descending by column-10, then ascending by column-1, and then ascending by column-2. A sample of the file is as follows:

10,0.1,8,182,1195,15,0.8592857142857143,0.34782608695652173,0.042105263157894736,0.07511737089201877
10,.20,8,182,1196,14,0.86,0.36363636363636365,0.042105263157894736,0.07547169811320754
10,.30,8,182,1196,14,0.86,0.36363636363636365,0.042105263157894736,0.07547169811320754
10,.40,8,182,1196,14,0.86,0.36363636363636365,0.042105263157894736,0.07547169811320754
10,.50,8,182,1196,14,0.86,0.36363636363636365,0.042105263157894736,0.07547169811320754
10,.60,8,182,1196,14,0.86,0.36363636363636365,0.042105263157894736,0.07547169811320754
10,.70,8,182,1196,14,0.86,0.36363636363636365,0.042105263157894736,0.07547169811320754
10,.80,8,182,1196,14,0.86,0.36363636363636365,0.042105263157894736,0.07547169811320754
10,.90,7,183,1196,14,0.8592857142857143,0.3333333333333333,0.03684210526315789,0.06635071090047392
10,1.00,6,184,1196,14,0.8585714285714285,0.3,0.031578947368421054,0.05714285714285715
20,0.1,16,174,1196,14,0.8657142857142858,0.5333333333333333,0.08421052631578947,0.14545454545454545
20,.20,16,174,1196,14,0.8657142857142858,0.5333333333333333,0.08421052631578947,0.14545454545454545
20,.30,16,174,1196,14,0.8657142857142858,0.5333333333333333,0.08421052631578947,0.14545454545454545
20,.40,16,174,1196,14,0.8657142857142858,0.5333333333333333,0.08421052631578947,0.14545454545454545
20,.50,16,174,1196,14,0.8657142857142858,0.5333333333333333,0.08421052631578947,0.14545454545454545
20,.60,16,174,1196,14,0.8657142857142858,0.5333333333333333,0.08421052631578947,0.14545454545454545
20,.70,16,174,1196,14,0.8657142857142858,0.5333333333333333,0.08421052631578947,0.14545454545454545
20,.80,16,174,1196,14,0.8657142857142858,0.5333333333333333,0.08421052631578947,0.14545454545454545
20,.90,16,174,1196,14,0.8657142857142858,0.5333333333333333,0.08421052631578947,0.14545454545454545
20,1.00,16,174,1196,14,0.8657142857142858,0.5333333333333333,0.08421052631578947,0.14545454545454545

and expected output is:

20,0.1,16,174,1196,14,0.865714285714286,0.533333333333333,0.084210526315790,0.145454545454545
20,0.2,16,174,1196,14,0.865714285714286,0.533333333333333,0.084210526315790,0.145454545454545
20,0.3,16,174,1196,14,0.865714285714286,0.533333333333333,0.084210526315790,0.145454545454545
20,0.4,16,174,1196,14,0.865714285714286,0.533333333333333,0.084210526315790,0.145454545454545
20,0.5,16,174,1196,14,0.865714285714286,0.533333333333333,0.084210526315790,0.145454545454545
20,0.6,16,174,1196,14,0.865714285714286,0.533333333333333,0.084210526315790,0.145454545454545
20,0.7,16,174,1196,14,0.865714285714286,0.533333333333333,0.084210526315790,0.145454545454545
20,0.8,16,174,1196,14,0.865714285714286,0.533333333333333,0.084210526315790,0.145454545454545
20,0.9,16,174,1196,14,0.865714285714286,0.533333333333333,0.084210526315790,0.145454545454545
20,1,16,174,1196,14,0.865714285714286,0.533333333333333,0.084210526315790,0.145454545454545
10,0.2,8,182,1196,14,0.860000000000000,0.363636363636364,0.042105263157895,0.075471698113208
10,0.3,8,182,1196,14,0.860000000000000,0.363636363636364,0.042105263157895,0.075471698113208
10,0.4,8,182,1196,14,0.860000000000000,0.363636363636364,0.042105263157895,0.075471698113208
10,0.5,8,182,1196,14,0.860000000000000,0.363636363636364,0.042105263157895,0.075471698113208
10,0.6,8,182,1196,14,0.860000000000000,0.363636363636364,0.042105263157895,0.075471698113208
10,0.7,8,182,1196,14,0.860000000000000,0.363636363636364,0.042105263157895,0.075471698113208
10,0.8,8,182,1196,14,0.860000000000000,0.363636363636364,0.042105263157895,0.075471698113208
10,0.1,8,182,1195,15,0.859285714285714,0.347826086956522,0.042105263157895,0.075117370892019
10,0.9,7,183,1196,14,0.859285714285714,0.333333333333333,0.036842105263158,0.066350710900474
10,1,6,184,1196,14,0.858571428571428,0.300000000000000,0.031578947368421,0.057142857142857

I can sort by the last column only by using the command:

sort -t, -k10 -n -r 0.txt

but I cannot figure out how to combine columns, and also how to mix ascending and descending orders. Could anyone please give me a solution?

I am running CentOS 5.3, sort version 5.97

Thanks in advance.

MensaWater 10-19-2009 02:15 PM

sort -t, -k10,10 -nk1,1 -nk2,2 -r 0.txt


All times are GMT -5. The time now is 06:02 AM.