linux sort command - Page 2 | Unix Linux Forums | Homework & Coursework Questions

  Go Back    


Homework & Coursework Questions Students must use and complete the template provided. If you don't, your post may be deleted! Special homework rules apply here.

linux sort command

Homework & Coursework Questions


Tags
sort by column, sort field

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #8  
Old 10-01-2012
demet8 demet8 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 12 November 2012, 5:15 PM EST
Posts: 8
Thanks: 6
Thanked 0 Times in 0 Posts
Linux Sort Command

Hey Don I think your right. I used that solution first but I felt it was too trivial initially. Now thinking about it again, I definitely think I read into the question a bit much. Thank you for your help. @bartus11 I will definitely try your script again and or use it for further reference. Thanks a lot.
Sponsored Links
    #9  
Old 10-02-2012
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 22 September 2014, 4:45 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 4,253
Thanks: 45
Thanked 817 Times in 644 Posts
There are some "sort" intrinsics to be considered and i think you haven't covered them all yet. This might not have any impact on your test file, but may well change the sorting order in another sample.

Lets see:

The default behavior of "sort" is to sort from the field/position given in te argument to the "-k" option to the end of line. That means:


Code:
sort -k 2 /some/file

will sort on field 2 first, in case f2 is equal on field 3, if this is equal too on field 4, etc. to the end of line. As you want to search on a last name - first name basis you have to state that:


Code:
sort -k 2,2 -k 1,1

You are lucky that you use only full fields, because it is possible to base sorting order on a sub-field starting at the n-th character of a certain field. Alas, character-numbering is sometimes 1-based and sometimes zero-based, depending on "-t" or "-b" being used - as i learned myself recently the hard way.

I hope this helps.

bakunin
Sponsored Links
    #10  
Old 10-02-2012
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 September 2014, 2:13 AM EDT
Location: San Jose, CA, USA
Posts: 4,679
Thanks: 180
Thanked 1,566 Times in 1,333 Posts
Quote:
Originally Posted by bakunin View Post
There are some "sort" intrinsics to be considered and i think you haven't covered them all yet. This might not have any impact on your test file, but may well change the sorting order in another sample.

Lets see:

The default behavior of "sort" is to sort from the field/position given in te argument to the "-k" option to the end of line. That means:


Code:
sort -k 2 /some/file

will sort on field 2 first, in case f2 is equal on field 3, if this is equal too on field 4, etc. to the end of line. As you want to search on a last name - first name basis you have to state that:


Code:
sort -k 2,2 -k 1,1

You are lucky that you use only full fields, because it is possible to base sorting order on a sub-field starting at the n-th character of a certain field. Alas, character-numbering is sometimes 1-based and sometimes zero-based, depending on "-t" or "-b" being used - as i learned myself recently the hard way.

I hope this helps.

bakunin
Very close... According to the standards (and the sort utilities I've used tend to follow the standards pretty closely), what you said about sort -k 2 is exactly correct. But, sort -k 2,2 sorts on the 2nd field and if two or more lines compare equal on that field, the entire line is used as the secondary sort key. So sort -k 2,2 performs exactly the same sort as sort -k 2,2 -k 1,1 .
    #11  
Old 10-19-2012
demet8 demet8 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 12 November 2012, 5:15 PM EST
Posts: 8
Thanks: 6
Thanked 0 Times in 0 Posts
Thanks everyone. I appreciate your help.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
LINUX SORT command chops results Yahalom UNIX for Dummies Questions & Answers 4 04-04-2012 09:44 AM
sort command in centos linux os jpachar Linux 1 02-22-2012 04:52 AM
linux sort command produces strange output ajb UNIX for Dummies Questions & Answers 3 08-11-2011 12:17 PM
How to Sort Floating Numbers Using the Sort Command? daniel.gbaena Shell Programming and Scripting 7 05-16-2010 11:20 AM
Linux Sort command itisijayare UNIX for Dummies Questions & Answers 1 04-03-2009 02:05 PM



All times are GMT -4. The time now is 02:19 AM.