LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-21-2003, 06:43 AM   #1
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
D[a]mned sort


I am highly frustrated by the behaviour of the standard unix sort command, which seems to always sort on all fields of a line, even if I specify one field to sort on.

If input of sort is like:
ON user2
OFF user2
ON user1

'cat input | sort +1' will always produce the following output:
ON user1
OFF user2
ON user2

Note that the output is also sorted on field0, though noone told sort to sort on field0.
Is there a way to tell sort not to sort on a specific field, i.e. leaving the lines in the positional order, instead?

My intention is that the sort output should be like this:
ON user1
ON user2
OFF user2
(i.e. it is not sorted on field0, only on field1)
 
Old 09-21-2003, 07:25 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
man sort says:

-s stabilize sort by disabling last resort comparison

cat infile.sort
ON user2
OFF user2
ON user1

$ sort -s +1 infile.sort
ON user1
ON user2
OFF user2

Last edited by druuna; 09-21-2003 at 07:27 AM.
 
Old 09-21-2003, 09:02 AM   #3
idaho
Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: RedHat, Libranet
Posts: 438

Rep: Reputation: 30
try sort -k 2,2

-k specifies the key field to sort on. The n,m argument specify the first and the last fields to sort on.
 
Old 09-21-2003, 03:42 PM   #4
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Original Poster
Rep: Reputation: 58
Thank you for the tips, both worked.
I already tried out a sort -k command, which (now I know) did not work as I thought the first data is field 0, the second is field 1, so tried sort -k 1,1 and dropped the idea when it did not work.

Last edited by J_Szucs; 09-21-2003 at 03:46 PM.
 
  


Reply



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
C++ Sort() bendeco13 Programming 2 11-02-2005 11:26 PM
how to sort the output of ls bahadur Programming 18 03-28-2005 07:08 PM
Hello (sort of...) Gethyn LinuxQuestions.org Member Intro 1 10-15-2004 12:10 PM
sort pantera Programming 5 05-26-2004 07:36 PM
Bubble sort? Becks Programming 4 09-12-2003 01:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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