|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Code:
# sort -k2 testfile Code:
MARCH6 MARCH2 MARCH2 MARCH326 MARCH9 MARCH4 MARCH1 MARCH5 MARCH821 MARCH7 cheers, Devaraj Takhellambam |
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
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
|
|||
|
|||
|
Try Code:
$ sort -n -k2.7 file Reason: man sort : Quote:
|
| The Following User Says Thank You to RudiC For This Useful Post: | ||
John K (03-15-2013) | ||
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
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 | |
|
|
#7
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|