Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I would like know how to sort the second last column ? It has the word "days=" in front of the number. I'm able to get the column with awk '{print $(NF-1)}', but sorting is wrong.

    457000      (test2) && (SGD||RMB||HKD||YEN)    days=5000    71
    37622 (this is || test1)&&(SGD||HKD||RMB) days=630 40010 
    43944 (this is)&&(SGD||HKD) days=80 102732 
    79378 (this is||test2)&&(HKD||RMB)   days=800 205425 

The result I'm looking for is

    43944 (this is)&&(SGD||HKD) days=80 102732 
    37622 (this is || test1)&&(SGD||HKD||RMB) days=630 40010 
    79378 (this is||test2)&&(HKD||RMB) days=800 205425
    457000      (test2) && (SGD||RMB||HKD||YEN)    days=5000    71
share|improve this question
add comment

1 Answer 1

up vote 8 down vote accepted

A bit hacky, but it ought to work:

awk '{print substr($(NF-1), 6), $0}' file | sort -g | cut -d' ' -f2-

It duplicates the second-to-last column at the front (dropping the days=), sorts numerically, and then removes the first column (the duplicated second-to-last one).

share|improve this answer
    
Yes it works. Thanks Kevin. –  user11496 Mar 20 '12 at 3:23
    
Just by the way, Python people call this "Decorate-Sort-Undecorate" or "DSU". They've added specific features to the language to enable more uses of DSU pattern. –  Bruce Ediger Mar 20 '12 at 16:50
    
In Perl, this is called a Schwartzian transform, after Randal Schwartz. –  daniel kullmann Mar 20 '12 at 18:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.