Sort command question | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Sort command question

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-05-2013
Drenhead Drenhead is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 7 August 2014, 4:50 PM EDT
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
Sort command question

I have a question about what the sort command is doing.

Here is some sample data:


Code:
348201310013RVE         2
600201310013GFJ        70
3302013020101NS        40
600201309013GFJ        70

The sort command that is running is as follows:


Code:
sort -k 1,3 -k 12,4 input.txt > output.txt

I think it is sorting it by the first 3 bytes, and then column 12 for 4 bytes?? Is that correct? I'm a little confused on the syntax.

Thanks for all help.
Sponsored Links
    #2  
Old 11-05-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 30 September 2014, 7:25 PM EDT
Location: Saskatchewan
Posts: 19,479
Thanks: 795
Thanked 3,286 Times in 3,081 Posts
Columns 1 through 3, then columns 12 through 4. I'm not sure that command actually makes sense for the data as given.
Sponsored Links
    #3  
Old 11-05-2013
Drenhead Drenhead is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 7 August 2014, 4:50 PM EDT
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
Are you saying that -k 4,12 would do the same thing? Would that make more sense?
    #4  
Old 11-05-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 30 September 2014, 7:25 PM EDT
Location: Saskatchewan
Posts: 19,479
Thanks: 795
Thanked 3,286 Times in 3,081 Posts
I'm saying that the data as given doesn't even have 12 columns, so I'm unsure what this statement's for.
Sponsored Links
    #5  
Old 11-05-2013
Drenhead Drenhead is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 7 August 2014, 4:50 PM EDT
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
Ok. I think it is trying to sort byte 1 for 3 bytes, then byte 12 for 4 bytes. How would that statement work?
Sponsored Links
    #6  
Old 11-05-2013
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 1 October 2014, 4:00 AM EDT
Location: San Jose, CA, USA
Posts: 4,761
Thanks: 181
Thanked 1,588 Times in 1,352 Posts
The sort utility key field specifiers -k 1,3 and -k 12,4 are specifying ranges of fields (not output print columns). To sort on the 1st three characters (still not print columns) on the line as the primary sort key and the 12th through the 15th characters of the 1st field as the secondary sort key the way to specify it would be:

Code:
sort -k1.1,1.3 -k1.12,1.15 input.txt >output.txt

which would save:

Code:
3302013020101NS        40
348201310013RVE         2
600201309013GFJ        70
600201310013GFJ        70

in output.txt for the given sample input. Note that when all given sort keys give two or more lines the same sort order (as in the last two lines here), the tie is broken by using the entire line as a final increasing order alphanumeric sort key.

PS Note also that sort works on text, not binary data. It sorts characters; not bytes. If the file you're sorting is ASCII it might not matter; but if your text contains UTF-8 multibyte characters; it makes a big difference.

Last edited by Don Cragun; 11-05-2013 at 05:33 PM.. Reason: Note byte != character.
The Following User Says Thank You to Don Cragun For This Useful Post:
Drenhead (11-05-2013)
Sponsored Links
    #7  
Old 11-05-2013
Drenhead Drenhead is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 7 August 2014, 4:50 PM EDT
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
Great. That makes a lot more sense to me now. Not sure what they were trying with the previous code.

Thanks for the 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
Sort command question newbie2010 UNIX for Dummies Questions & Answers 3 08-11-2013 06:00 PM
Sort question. nua7 Shell Programming and Scripting 5 05-22-2012 02:39 PM
New to Unix command line and have a question about the "sort" command budfoxcat UNIX for Dummies Questions & Answers 1 11-28-2011 12:46 AM
Is it Possible to sort a list of hexadecimal numbers using "sort" command? Kesavan Shell Programming and Scripting 9 08-21-2011 08:33 AM
How to Sort Floating Numbers Using the Sort Command? daniel.gbaena Shell Programming and Scripting 7 05-16-2010 11:20 AM



All times are GMT -4. The time now is 04:12 AM.