LinuxQuestions.org
Visit Jeremy's Blog.
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-18-2009, 09:48 AM   #1
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 141

Rep: Reputation: 16
sort by multiple columns


hi all I have file:

154 alfonz novak
154 michael cores
62 jan nedorost
62 martin adrov

and I want to sort by first column numericaly and then by third column alphabeticaly. so output should be:

62 martin adrov
62 jan nedorost
154 michael cores
154 alfonz novak

I tried
Code:
sort -k1n,3 sort
but it gives me:

62 jan nedorost
62 martin adrov
154 alfonz novak
154 michael cores

what I am doing wrong ? Thanks
 
Old 10-18-2009, 10:03 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Note: This way to do it is wrong, see below.

Code:
bash-3.1$ echo '
154 alfonz novak
154 michael cores
62 jan nedorost
62 martin adrov
' | sort -k1r
62 martin adrov
62 jan nedorost
154 michael cores
154 alfonz novak
so just:

Code:
sort -k1r sort

Last edited by H_TeXMeX_H; 10-18-2009 at 10:40 AM. Reason: This is wrong....
 
Old 10-18-2009, 10:13 AM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
I think this will cover what you're asking for:

Code:
sort -k1,1r -k3 names
(Edited: first shot no good.)

Last edited by Telemachos; 10-18-2009 at 10:19 AM.
 
Old 10-18-2009, 10:25 AM   #4
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by H_TeXMeX_H View Post
Code:
bash-3.1$ echo '
154 alfonz novak
154 michael cores
62 jan nedorost
62 martin adrov
' | sort -k1r
62 martin adrov
62 jan nedorost
154 michael cores
154 alfonz novak
so just:

Code:
sort -k1r sort
Unless I'm confused, this only reverses the numeric sort on the first field. By coincidence that works on his four names, but what if we add others:

Code:
telemachus ~ $ cat names 
154 alfonz novak
62 ashley barone
154 michael cores
62 jan nedorost
154 albert neumann
62 martin adrov

telemachus ~ $ sort -k1r names 
62 martin adrov
62 jan nedorost
62 ashley barone
154 michael cores
154 alfonz novak
154 albert neumann
You need to add a second -k flag to deal with his second request (namely sort alphabetically by last name after sorting numberically by first field).
 
Old 10-18-2009, 10:40 AM   #5
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Oh, yeah, oops. You're right, there should be another one, my bad.
 
Old 10-18-2009, 03:35 PM   #6
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 141

Original Poster
Rep: Reputation: 16
Thanks guys, I did not know I can put another -k options. so my code looks like
Code:
sort -k1n -k3 names
62 martin adrov
62 jan nedorost
154 michael cores
154 alfonz novak
I tried that with one -k options and comma separate columns (according to man page that way I understood syntax).

Just for curiosity what is difference between "-k1n,3" and "-k1n -k3" teoreticaly both should do the same thing but practice is different

Last edited by wakatana; 10-19-2009 at 03:54 PM.
 
  


Reply


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
Multiple field sort (quick explanation) bioinformatics_guy Linux - Newbie 3 01-12-2009 09:09 AM
PHP MySql search over multiple columns a_m0d Programming 3 07-09-2008 07:19 AM
Unix sort on multiple fields belorion Programming 8 11-12-2007 01:15 PM
Is there a line limit with the sort utility? Trying to sort 130 million lines of text gruffy Linux - General 4 08-10-2006 08:40 PM
how do I Add multiple columns mysql in postgres Bheki Linux - Software 1 08-28-2003 02:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:05 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
Open Source Consulting | Domain Registration