[Solved] sort on numeric part of field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] sort on numeric part of field
# 1  
Old 08-29-2012
[Solved] sort on numeric part of field

I have ran into a heavy case of PEBCAK*) and could need some advice on what i do wrong:

OS is Linux (kernel 2.6.35), sort --version reports "8.5" from 2010, shell is ksh.

Originally i had a file with with the following structure:

Code:
hdisk1 yyy
hdisk2 yyy
hdisk3 yyy
hdisk4 yyy
hdisk5 yyy
hdisk6 yyy
hdisk7 yyy
hdisk7 yyy
hdisk8 yyy
hdisk9 yyy
hdisk10 yyy
hdisk11 yyy
hdisk12 yyy

I wanted to sort the file descendingly based on the numeric part if field 1, so i used the command:

Code:
sort -srn -k1.6,1 <file>

which worked as expected with starting line "hdisk12" and ending line "hdisk1".

Now the file format changed and it looks like this:

Code:
xxx hdisk1 yyy
xxx hdisk2 yyy
xxx hdisk3 yyy
xxx hdisk4 yyy
xxx hdisk5 yyy
xxx hdisk6 yyy
xxx hdisk7 yyy
xxx hdisk7 yyy
xxx hdisk8 yyy
xxx hdisk9 yyy
xxx hdisk10 yyy
xxx hdisk11 yyy
xxx hdisk12 yyy

Some fixed-length information at the beginning of each line added. But when i changed the command like below the output was not as expected:

Code:
# sort -srn -k2.6,2 <file>
xxx hdisk9 yyy
xxx hdisk8 yyy
xxx hdisk7 yyy
xxx hdisk6 yyy
xxx hdisk5 yyy
xxx hdisk4 yyy
xxx hdisk3 yyy
xxx hdisk2 yyy
xxx hdisk12 yyy
xxx hdisk11 yyy
xxx hdisk10 yyy
xxx hdisk1 yyy

Nothing i have tried so far worked and the according to the man page i *think* it should.

I'd be indebted for any pointers. Thanks.

bakunin
________
*) problem exists between keyboard and chair
# 2  
Old 08-29-2012
From man sort:
Code:
KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a field number and C a character position  in
       the field; both are origin 1, and the stop position defaults to the line's end.  If neither -t nor -b is in effect, char‐
       acters in a field are counted from the beginning of the preceding whitespace.  OPTS is one or more single-letter ordering
       options  [bdfgiMhnRrV],  which override global ordering options for that key.  If no key is given, use the entire line as
       the key.

So if you change your command to sort -srn -k2.7,2 file, it should do the trick. Or try sort -t' ' -srn -k2.6,2 file.

Last edited by elixir_sinari; 08-29-2012 at 10:01 AM..
# 3  
Old 08-29-2012
Oops. *blushes*

You are right, i have found that it works with specifying "-t ' '" already, but was still wondering why. Would i have not only read the man page but also understood what it says i should have known.

Thanks for this.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with sort word and general numeric sort at the same time

Input file: 100%ABC2 3.44E-12 USA A2M%H02579 0E0 UK 100%ABC2 5.34E-8 UK 100%ABC2 3.25E-12 USA A2M%H02579 5E-45 UK Output file: 100%ABC2 3.44E-12 USA 100%ABC2 3.25E-12 USA 100%ABC2 5.34E-8 UK A2M%H02579 0E0 UK A2M%H02579 5E-45 UK Code try: sort -k1,1 -g -k2 -r input.txt... (2 Replies)
Discussion started by: perl_beginner
2 Replies

2. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

3. Shell Programming and Scripting

Sort numeric order

Hi I am using this cat substitutionFeats.txt | gawk '{$0=gensub(/\t/,"blabla",1);print}' | gawk '{print length, $0}' | sort -n | sort -r and the "sort -n" command doesn't work as expected: it leads to a wrong ordering: 64 Adjustable cuffs 64 Abrasion- 64 Abrasion pas 647 Sanitized 647... (4 Replies)
Discussion started by: louisJ
4 Replies

4. UNIX for Dummies Questions & Answers

sort files by numeric filename

dear all, i have .dat files named as: 34.dat 2.dat 16.dat 107.dat i would like to sort them by their filenames as: 2.dat 16.dat 34.dat 107.dat i have tried numerous combinations of sort and ls command (in vain) to obtain : 107.dat 16.dat 2.dat 34.dat (1 Reply)
Discussion started by: chen.xiao.po
1 Replies

5. Shell Programming and Scripting

Sort help on non numeric field

Hi, I am unable to sort data on the first field $cat t Jim,212121,Seattle Bill,404404,Seattle Steve,246810,Nevada Scott,212277,LosAngeles Jim,212121,Ohio sort -t"," -k1,2 t Bill,404404,Seattle Jim,212121,Ohio Jim,212121,Seattle Scott,212277,LosAngeles Steve,246810,Nevada (7 Replies)
Discussion started by: Shivdatta
7 Replies

6. Shell Programming and Scripting

Perl script to sort data on second numeric field

Hi, I'm a learner of PERL programming. I've a input file with the below data: SWAT_5, 1703, 2010-09-21 SWAT_6, 2345, 2010-09-21 SWAT_7, 1792, 2010-09-21 SWAT_8, 1662, 2010-09-21 SWAT_9, 1888, 2010-09-21 VXHARP_1, 171, 2010-09-21 I need to sort this data based on the second... (6 Replies)
Discussion started by: ganapati
6 Replies

7. Shell Programming and Scripting

Sort alpha on 1st field, numerical on 2nd field (sci notation)

I want to sort alphabetically on the first field and sort in descending numerical order on the 2nd field. With a normal "sort -r -n" it does this: abc ||| 5e-05 ||| bla abc ||| 3 ||| ble def ||| 1 ||| abc def ||| 0.2 ||| def As you can see it ignores the fact that 5e-05 is actually 0.00005... (1 Reply)
Discussion started by: FrancoisCN
1 Replies

8. Shell Programming and Scripting

Numeric sort error

Hello all I have data like below where the column with values (PRI, SEC ) is the char field and the rest are Numeric Fields. 200707,9580,58,7,2,1,PRI,1,1,137,205594,0,5,10,-45.51,-45.51 200707,9580,58,7,2,1,SEC,1,1,137,205594,0,5,10,-45.51,45.51... (1 Reply)
Discussion started by: vasuarjula
1 Replies

9. Shell Programming and Scripting

how to numeric sort on field time

i want to sort time field given by who command as a whole i have tried like this who|sort -n +4 -5 (1 Reply)
Discussion started by: rahulspatil_111
1 Replies

10. UNIX for Dummies Questions & Answers

Wanted to eliminate numeric part from a filename

Hi, I have a filename called XYZ12345.txt.I just want to eliminate numeric and have only XYZ.txt. How can i do it ? Regards, Sona. (8 Replies)
Discussion started by: Sona
8 Replies
Login or Register to Ask a Question