Go Back   The UNIX and Linux Forums > Homework and Emergencies > Homework & Coursework Questions


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.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #8  
Old 10-01-2012
Registered User
 
Join Date: Sep 2012
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.
    #9  
Old 10-02-2012
bakunin bakunin is online now Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 3,298
Thanks: 27
Thanked 455 Times in 354 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
    #10  
Old 10-02-2012
Registered User
 
Join Date: Jul 2012
Location: San Jose, CA
Posts: 1,475
Thanks: 62
Thanked 531 Times in 465 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
Registered User
 
Join Date: Sep 2012
Posts: 8
Thanks: 6
Thanked 0 Times in 0 Posts
Thanks everyone. I appreciate your help.
Closed Thread

Tags
sort by column, sort field

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 04:27 PM.