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


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 06-12-2009
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 therefore smaller than 3. The output I want would be

abc ||| 3 ||| ble
abc ||| 5e-05 ||| bla
def ||| 1 ||| abc
def ||| 0.2 ||| def
# 2  
Old 06-12-2009
Code:
perl -e'
    print sort {
             ( split /\|\|\|/, $a )[0] cmp( split /\|\|\|/, $b )[0]
          || ( split /\|\|\|/, $b )[1] <=> ( split /\|\|\|/, $a )[1]
    } <>;
    ' infile


Last edited by radoulov; 06-12-2009 at 01:03 PM..
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Combine Similar Output from the 2nd field w.r.t 1st Field alvinoo UNIX for Dummies Questions & Answers 1 04-28-2016 04:11 AM
Sort array elements from same field beca123456 UNIX for Dummies Questions & Answers 5 06-10-2013 05:25 AM
Sort field and uniq sabercats Shell Programming and Scripting 3 12-05-2012 03:15 PM
Sorting Date Field with Sort -k :/ Glitch100 Shell Programming and Scripting 17 01-05-2012 12:09 PM
how to find the 2nd field pareshpatra Shell Programming and Scripting 1 08-18-2011 10:24 AM
Sort help on non numeric field Shivdatta Shell Programming and Scripting 7 06-18-2011 11:24 AM
Append 1st field from a file into 2nd field of another file amurib Shell Programming and Scripting 1 12-25-2010 05:13 AM
Appending 1st field in a file into 2nd field in another file amurib Shell Programming and Scripting 0 12-25-2010 04:49 AM
Sort on basis of particular field manishma71 UNIX for Advanced & Expert Users 9 07-19-2010 06:16 AM
Sort field by date mm/dd/yyyy csierra UNIX for Dummies Questions & Answers 6 03-01-2010 09:11 PM
Sort two columns in a field, one of them being a date sweta_doshi Shell Programming and Scripting 5 07-28-2009 06:15 AM
get a name according to the 2nd field melanie_pfefer Shell Programming and Scripting 4 01-28-2009 11:47 AM
sort columns by field kamel.seg Shell Programming and Scripting 4 02-20-2008 05:50 AM
how to numeric sort on field time rahulspatil_111 Shell Programming and Scripting 1 04-27-2007 11:52 AM