LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 04-26-2009, 03:57 PM   #1
zombieite
LQ Newbie
 
Registered: Sep 2006
Posts: 3

Rep: Reputation: 0
Can I use GNU sort to sort one field in order, another in reverse?


My file looks like this:

Ann 6
John 5
Betty 5

I want it to look like this:

Ann 6
Betty 5
John 5

If I use 'sort -r -k2 -k1' or even 'sort -rk2 -k1', I get:

Ann 6
John 5
Betty 5

because it sorts /all/ fields in reverse. I only want to sort k2 (the second field) in reverse, but when I have to sort based on k1 (the first field--that is, when the second field is a tie) I want to sort the first field in order, not reverse order.

Can anyone help? Or is this not possible using GNU sort?
 
Old 04-26-2009, 06:04 PM   #2
sdlor
LQ Newbie
 
Registered: Mar 2008
Location: The Great NorthWest
Distribution: Fedora 14, Unbuntu 8.04
Posts: 8

Rep: Reputation: 0
Wouldn't just an alphabetical sort give you the desired output??
 
Old 04-26-2009, 06:12 PM   #3
zombieite
LQ Newbie
 
Registered: Sep 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Oh yes, sorry, bad example. Let me try again:

My file looks like this:

Zack 6
John 5
Betty 5

I want it to look like this:

Zack 6
Betty 5
John 5
 
Old 04-26-2009, 07:56 PM   #4
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 241

Rep: Reputation: 45
From info coreutils 'sort invocation':
Quote:
A position in a sort field specified with `-k' may have any of the
option letters `Mbdfinr' appended to it, in which case the global
ordering options are not used for that particular field.
So this should work:
Code:
sort -k2r -k1
 
Old 04-27-2009, 12:23 AM   #5
zombieite
LQ Newbie
 
Registered: Sep 2006
Posts: 3

Original Poster
Rep: Reputation: 0
That was it! Thank you so much!
 
  


Reply

Tags
field, reverse, sort


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
How do I do filtering in Perl (keep sort order and sort again by another means)? RavenLX Programming 9 12-19-2008 10:12 AM
Suggestion: Let's sort them in a,b,c... order fuelinux LQ Suggestions & Feedback 8 01-20-2008 10:35 AM
Sort order in nautilus Patrick K Ubuntu 4 06-12-2007 08:53 PM
Sort File by Field - but with a Twist! ;) moo-cow Programming 8 06-12-2006 11:26 AM


All times are GMT -5. The time now is 05:20 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration