LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 06-23-2005, 01:48 AM   #1
smart_sagittari
LQ Newbie
 
Registered: Apr 2005
Posts: 26

Rep: Reputation: 16
Sort file based on only ONE colum


I have a file which looks something like this:

a 123 sdfg
a 123 asdf
b 012 fsd
a 123 afds
b 012 asf

Now I want to sort this based on just the second field [the numeric field], at the same time, not altering the existing internal order for each number.

To be precise, the output should look something like this:

b 012 fsd
b 012 asf
a 123 sdfg
a 123 asdf
a 123 afds

How can I achieve this using the sort command? I tried using the

Code:
sort +1 -2 -k 1,1 <filename>
command. But the output that I got was sorted first based on the second field [which is fine], but then based on the rest of the line for that particular number, which is:

b 012 asf
b 012 fsd
a 123 afds
a 123 asdf
a 123 sdfg
 
Old 06-23-2005, 01:03 PM   #2
hussar
Member
 
Registered: Oct 2003
Location: Heidelberg, Germany
Distribution: Slackware 11.0; Kubuntu 6.06; OpenBSD 4.0; OS X 10.4.10
Posts: 345

Rep: Reputation: 30
I haven't completely figured it out yet, but I believe the problem has to do with the way sort deals with blanks. I need to go back and reread that part of the info page a couple of more times. Anyway, this combination works for me:

sort -k 2b,2b -k 3b,3r <filename>

Try it on your system and see what you get.

BTW, according to the info pages at `info coreutils sort`, the "+1 -2" stuff is deprecated:

" On older systems, `sort' supports an obsolete origin-zero syntax
`+POS1 [-POS2]' for specifying sort keys. POSIX 1003.1-2001 (*note
Standards conformance: does not allow this; use `-k' instead."

Last edited by hussar; 06-25-2005 at 04:48 AM.
 
Old 06-27-2005, 07:49 AM   #3
smart_sagittari
LQ Newbie
 
Registered: Apr 2005
Posts: 26

Original Poster
Rep: Reputation: 16
Doesnt the -r option stand for reverse? It worked in this case because the sample file that I gave didnt really have too many entries. However, if you add one more row and try it out, it shall fail. Is there any other solution? For some strange, sort doesnt seem to understand that it needs to restrict itself to just the first column when specified to. I very badly need this info ( Please help me out.
 
Old 07-05-2005, 02:32 PM   #4
hussar
Member
 
Registered: Oct 2003
Location: Heidelberg, Germany
Distribution: Slackware 11.0; Kubuntu 6.06; OpenBSD 4.0; OS X 10.4.10
Posts: 345

Rep: Reputation: 30
Sorry to be so long getting back. Life intruded.

Yes, the r stands for reverse. The b stands for "ignore leading blanks."

I added another row to the list, and the command I mentioned to you still works.

Here is my test file, sorttest:

a 123 sdfg
a 123 asdf
b 012 fsd
a 123 afds
b 012 asf
a 123 fdsa

Here is the command I used:

sort -k 2b,2b -k 3b,3r sorttest

Here are the results I got:

b 012 fsd
b 012 asf
a 123 sdfg
a 123 fdsa
a 123 asdf
a 123 afds

Isn't that what you were trying to do? Have I misunderstood the problem?
 
Old 07-06-2005, 01:29 AM   #5
smart_sagittari
LQ Newbie
 
Registered: Apr 2005
Posts: 26

Original Poster
Rep: Reputation: 16
Quote:
Originally posted by hussar
Here are the results I got:

b 012 fsd
b 012 asf
a 123 sdfg
a 123 fdsa
a 123 asdf
a 123 afds

Thanks for reverting back. But the results that you have got are not what I desired. The output should have the second column sorted first, and then sort ONLY the first column. So the output given your sample input would look like:

b 012 fsd
b 012 asf
a 123 sdfg
a 123 asdf
a 123 afds
a 123 fdsa

Please note the last three lines in the results.
 
Old 07-07-2005, 12:32 PM   #6
hussar
Member
 
Registered: Oct 2003
Location: Heidelberg, Germany
Distribution: Slackware 11.0; Kubuntu 6.06; OpenBSD 4.0; OS X 10.4.10
Posts: 345

Rep: Reputation: 30
When sort finds two or more keys equal to each other, sort does a "last resort comparison." Here's a quote from `info coreutils sort`:

"Finally, as a last resort when all keys compare equal (or if no
ordering options were specified at all), `sort' compares the entire
lines. The last resort comparison honors the `--reverse' (`-r') global
option. The `--stable' (`-s') option disables this last-resort
comparison so that lines in which all fields compare equal are left in
their original relative order. If no fields or global options are
specified, `--stable' (`-s') has no effect."

Since you are only sorting on one key, and there are two or more lines with the same value in that key column, sort then uses the entire line to sort the matching lines within themselves.

A possible solution to your problem is the use of the "--stable" option. Observe:

Here is my test file, sorttest:

a 123 sdfg
a 123 asdf
b 012 fsd
a 123 afds
b 012 asf
a 123 fdsa

Here is the command I used:

sort -k 2b,2b --stable sorttest

Here are my results:

b 012 fsd
b 012 asf
a 123 sdfg
a 123 asdf
a 123 afds
a 123 fdsa

How's that?
 
Old 07-08-2005, 12:25 AM   #7
smart_sagittari
LQ Newbie
 
Registered: Apr 2005
Posts: 26

Original Poster
Rep: Reputation: 16
Quote:
Originally posted by hussar

Here are my results:

b 012 fsd
b 012 asf
a 123 sdfg
a 123 asdf
a 123 afds
a 123 fdsa

How's that?
Simply brilliant! Thanks a lot!
 
  


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
How can I sort the lines in a file? windhair Linux - Software 2 11-17-2005 08:37 AM
what sort of file attribute is this: b--Srws-wt BrianK Linux - General 6 12-17-2004 11:26 PM
ls -l second colum in output praveenv Linux - Newbie 1 08-26-2004 07:37 AM
Reverse Sort Text File BxBoy Linux - General 1 08-02-2004 10:13 AM
Need to sort mp3's and create playlists based on genre cav Linux - General 11 07-06-2003 10:04 PM


All times are GMT -5. The time now is 04:48 AM.

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