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

1 Answer 1

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

2
  • 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.
    – user732
    Mar 20, 2012 at 16:50
  • In Perl, this is called a Schwartzian transform, after Randal Schwartz. Mar 20, 2012 at 18:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .