I'm running a script (that I have no control of) to get the following output. I want to sort by the last(3rd) column. Each column is separate by spaces, and the 2nd column includes spaces/symbols.

    > ./script
    37622       (this is || test1)&&(SGD||HKD||RMB)     40010
    43944       (this is)&&(SGD||HKD)    102732
    79378       (this is||test2)&&(HKD||RMB)    205425
    457000      (test2) && (SGD||RMB||HKD||YEN)        71
    559658      (test1||test2)&&(RMB||YEN||SGD)     14043

I tried to use sort -k, but it doesn't work. Then I found this question - How to numerical sort by last column? - the solution provided is

awk '{print $NF,$0}' file.txt | sort -nr | cut -f2- -d' '

My question is: how do I make use of this when I run the script?

    > ./script | <something??>

Thank you.

  • Very close to what you wrote: ./script | awk '{print $NF,$0}' | sort -nr | cut -f2- -d' ' Mar 16, 2012 at 2:27
  • possible duplicate of <a>unix.stackexchange.com/questions/10524/…>
    – user14039
    Mar 16, 2012 at 3:53
  • A "possible", but he actually mentions the "duplicate" question in his own question... His problem is really not related to the sort. The issue lies in how to pass the output of his script to awk..
    – Peter.O
    Mar 16, 2012 at 7:56

1 Answer 1



You can adapt the linked pipe in a straight forward way:

$ ./script | awk '{ print $NF,$0 }' | sort -k1,1 -n | cut -f2- -d' '

In awk the expression $x references the x-th column of the current line (starting with 1) - and the predefined variable NF stores the number of columns of the current line, thus print $NF,$0 prints for each line the last column and the complete line (because $0 denotes the complete line). The cut command then outputs the 2nd to the last column of each line.

The -k1,1 part of sort means that only the first column is used as sort key - this only makes a difference when more than one line have the same value in the first column. Without -k1,1 the following columns will influence the relative order (as secondary and so on sorting key) in that case. With -k1,1 only the first column is used as sorting key - and the relative order of lines with the same key is not changed (i.e. a stable sort is performed).


Alternatively you can solve it via sort and sed:

$ ./script | sed 's/^\(.\+[ \t]\+\)\([0-9]\+ *\)$/\2 \1/' | \
     sort -k1,1 -n | sed 's/^\([0-9]\+\) \(.\+\)$/\2 \1/'

Where the newline at the end of the first line is escaped via \ - you can remove that \ and enter the pipe as one line.

The idea is to first move the last column to the front, sort by the first column and then put it to the back again.

It is assumed that the last column is separated via white space, i.e. [ \t]\+ (spaces or tabs).

The sed expressions do the swapping via group references (e.g. \2 \1) - the groups are marked in the pattern via escaped parentheses: \(...\)

  • What if I want the sort to happen on 3rd column? It has the word "days=" in front of the number? 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
    – user11496
    Mar 19, 2012 at 9:17
  • 1
    @user11496, please post a new question - the line breaks in the comment make it hard do understand. Mar 19, 2012 at 15:36
  • You are right. I've posted a new one with subject "Linux sort second last column"
    – user11496
    Mar 20, 2012 at 1:48

You must log in to answer this question.

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