LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 10:48 AM   #1
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 133

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, 11:03 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
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 11:40 AM. Reason: This is wrong....
 
Old 10-18-2009, 11:13 AM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
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 11:19 AM.
 
Old 10-18-2009, 11:25 AM   #4
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
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, 11:40 AM   #5
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

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

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 04: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 10:09 AM
PHP MySql search over multiple columns a_m0d Programming 3 07-09-2008 08:19 AM
Unix sort on multiple fields belorion Programming 8 11-12-2007 02: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 09:40 PM
how do I Add multiple columns mysql in postgres Bheki Linux - Software 1 08-28-2003 03:30 PM

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

All times are GMT -5. The time now is 12:56 AM.

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