Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Sorting based on the second field

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-15-2013
John K John K is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 18 November 2015, 12:45 AM EST
Posts: 69
Thanks: 85
Thanked 1 Time in 1 Post
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 Unix and Linux 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 Unix and Linux 03-15-2013
anbu23's Unix or Linux Image
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 18 July 2016, 7:20 AM EDT
Location: Bangalore,India
Posts: 2,152
Thanks: 21
Thanked 159 Times in 150 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 Unix and Linux 03-15-2013
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 28 September 2016, 5:25 AM EDT
Location: Aachen, Germany
Posts: 9,395
Thanks: 184
Thanked 2,750 Times in 2,574 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 Unix and Linux 03-15-2013
John K John K is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 18 November 2015, 12:45 AM EST
Posts: 69
Thanks: 85
Thanked 1 Time in 1 Post
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 Unix and Linux 03-15-2013
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 28 September 2016, 5:25 AM EDT
Location: Aachen, Germany
Posts: 9,395
Thanks: 184
Thanked 2,750 Times in 2,574 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 Unix and Linux 03-15-2013
John K John K is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 18 November 2015, 12:45 AM EST
Posts: 69
Thanks: 85
Thanked 1 Time in 1 Post
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 Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 11:22 AM.