6

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
10

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).

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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