Sorting based on the second field | 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.

Sorting based on the second field

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-15-2013
John K John K is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 23 August 2014, 6:22 AM EDT
Posts: 47
Thanks: 50
Thanked 0 Times in 0 Posts
Sorting based on the second field

Oracle Enterprise Linux 6

This is my file. Two fields separated by space

Code:
$ cat testfile.txt
MARCH9 MARCH4
MARCH1 MARCH5
MARCH2 MARCH326
MARCH821 MARCH7
MARCH6 MARCH2
$
$

The following numeric sort, based on the first field's 6th character works as expected.

Code:
$
$ sort -n -k  1.6 testfile.txt
MARCH1 MARCH5
MARCH2 MARCH326
MARCH6 MARCH2
MARCH9 MARCH4
MARCH821 MARCH7
$

But the following attempt to sort the lines based on the second field's numeric character doesn't seem to work.
--- In the below example for "-k 2.6" , 2 denotes second field, 6 denotes the character based on which sort should happen (I have a numeric sort here)

Code:
$
$ sort -n -k  2.6 testfile.txt
MARCH1 MARCH5
MARCH2 MARCH326
MARCH6 MARCH2
MARCH821 MARCH7
MARCH9 MARCH4

Sponsored Links
    #2  
Old 03-15-2013
devtakh devtakh is offline Forum Advisor  
Unix Enthusiatic
 
Join Date: Oct 2007
Last Activity: 21 August 2013, 5:20 AM EDT
Location: Bangalore
Posts: 738
Thanks: 0
Thanked 7 Times in 7 Posts

Code:
# sort -k2 testfile


Code:
MARCH6 MARCH2
MARCH2 MARCH326
MARCH9 MARCH4
MARCH1 MARCH5
MARCH821 MARCH7


cheers,
Devaraj Takhellambam
Sponsored Links
    #3  
Old 03-15-2013
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 17 September 2014, 3:10 AM EDT
Location: Bangalore,India
Posts: 2,089
Thanks: 12
Thanked 131 Times in 126 Posts
Mention the delimiter

Code:
$ sort -t" " -n -k  2.6 file
MARCH6 MARCH2
MARCH9 MARCH4
MARCH1 MARCH5
MARCH821 MARCH7
MARCH2 MARCH326

The Following User Says Thank You to anbu23 For This Useful Post:
John K (03-15-2013)
    #4  
Old 03-15-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 17 September 2014, 4:41 PM EDT
Location: Aachen, Germany
Posts: 4,265
Thanks: 72
Thanked 1,030 Times in 976 Posts
Try
Code:
$ sort -n -k2.7 file

Reason:

man sort :
Quote:
POS is F[.C][OPTS], where F is the field number and C the character position in the field; both are origin 1. If neither -t nor -b
is in effect, characters in a field are counted from the beginning of the preceding whitespace.
The Following User Says Thank You to RudiC For This Useful Post:
John K (03-15-2013)
Sponsored Links
    #5  
Old 03-15-2013
John K John K is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 23 August 2014, 6:22 AM EDT
Posts: 47
Thanks: 50
Thanked 0 Times in 0 Posts
Thanks Anbu. You solution worked.
Thank you Rudic . You solution worked as well.

Code:
 
$ sort -n -k 2.7 myfile.txt
MARCH6 MARCH2
MARCH9 MARCH4
MARCH1 MARCH5
MARCH821 MARCH7
MARCH2 MARCH326

But what is 7 ? It should be sorting from 6th character onwards. Right ?
Sponsored Links
    #6  
Old 03-15-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 17 September 2014, 4:41 PM EDT
Location: Aachen, Germany
Posts: 4,265
Thanks: 72
Thanked 1,030 Times in 976 Posts
Did you read the quote from sort's man page?
The Following User Says Thank You to RudiC For This Useful Post:
John K (03-15-2013)
Sponsored Links
    #7  
Old 03-15-2013
John K John K is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 23 August 2014, 6:22 AM EDT
Posts: 47
Thanks: 50
Thanked 0 Times in 0 Posts
Got it. Its because of this . Right ?

Code:
 
If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace

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
sorting based on a field SkySmart Shell Programming and Scripting 2 05-17-2012 10:15 PM
Sorting data in file based on field in another file kasan0 UNIX for Dummies Questions & Answers 3 11-09-2011 03:59 AM
Sorting on two fields time field and number field pat4519 Shell Programming and Scripting 2 01-08-2010 02:45 PM
Sorting file by a field, and then by another field. Alexis Duarte Shell Programming and Scripting 5 11-22-2009 03:27 PM
Find top N values for field X based on field Y's value FrancoisCN Shell Programming and Scripting 1 05-29-2009 09:57 AM



All times are GMT -4. The time now is 09:41 AM.