LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-19-2009, 02:53 PM   #1
arshadul
LQ Newbie
 
Registered: Apr 2009
Distribution: CentOS 5.3, Ubuntu 8.0.4LTS
Posts: 4

Rep: Reputation: 0
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.
 
Old 10-19-2009, 03:15 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,578
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
sort -t, -k10,10 -nk1,1 -nk2,2 -r 0.txt
 
  


Reply

Tags
sorting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to make several columns in a text file of different lengths the same length part2 Mike_V Programming 11 05-15-2009 02:44 AM
how to make several columns in a text file of different lengths the same length Mike_V Programming 3 04-23-2009 10:17 PM
sorting columns in bash twistadias Linux - Newbie 8 08-25-2008 01:22 AM
delete columns 15 to 27 of a text file powah Programming 1 01-31-2007 03:03 AM
sorting text file - sort command man_linux Linux - General 16 08-09-2006 05:58 PM


All times are GMT -5. The time now is 09:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration