LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-25-2004, 03:50 AM   #1
pantera
Member
 
Registered: May 2004
Posts: 80

Rep: Reputation: 15
sort


how to sort a file according to a particular field
0.00
0.00
1
1
1
1
4
5
5
5
5.00
1.00
5.00
3.00
5.00
7.00

in the above file how can i sort it so that i can get
0.00
1.00
3.00
5.00
7.00
1
4
5
 
Old 05-25-2004, 03:59 AM   #2
zeropash
Member
 
Registered: Apr 2003
Location: Bangalore,India
Distribution: FC2, RHES, RH9, FC3, FC1, Slackware 3.0
Posts: 208

Rep: Reputation: 30
sort -nu filename
or sort -gu filename
0.00
1
3.00
4
5
7.00



if you consider 5 to be separate from 5.0 then use

sort -n filename | uniq
this will give output of the form
0.00
1
1.00
3.00
4
5
5.00
7.00
 
Old 05-25-2004, 10:07 AM   #3
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
sort +0.1 file | uniq
 
Old 05-26-2004, 07:21 AM   #4
zeropash
Member
 
Registered: Apr 2003
Location: Bangalore,India
Distribution: FC2, RHES, RH9, FC3, FC1, Slackware 3.0
Posts: 208

Rep: Reputation: 30
but wont that cause problems the moment you add say 1.10 to the list? ie the 1.10 will be placed behind 7.0?
 
Old 05-26-2004, 01:08 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,499

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Pantera, can you give more clues to the rest of us about the logic you want for the sort ?
 
Old 05-26-2004, 07:36 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
sort -u -t. -g -k 2,2 pantera.txt

Not quite the order you wanted, but close enough?
I'm sure it's possible to tweak this with a few extra
-r's, but I can't be bothered messing around with it :)

The alternative would be to split the file using awk
or grep into two files, sort those, and then merge
them.



Cheers,
Tink
 
  


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
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
D[a]mned sort J_Szucs Linux - Software 3 09-21-2003 03:42 PM
Bubble sort? Becks Programming 4 09-12-2003 01:28 PM


All times are GMT -5. The time now is 11: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