7

I want to sort the line accorfing to the last number just before the space. And this is a simplified example:

c3_abl_eerf_14 sasw
a.bla_haha_2 dnkww
s.hey_3 ddd

And this is the results I want:

a.bla_haha_2 dnkww
s.hey_3 ddd
c3_abl_eerf_14 sasw

I don't know how to do this, maybe by the command sort? And, sometimes I used the sort command, it may wrongly treat the 14 less than 2, I don't want this to happen.

1
  • 1
    You can not do this with sort, as it will only accept a single character as field separator. In order to sort numerically, use sort -n. Jun 8, 2016 at 8:12

2 Answers 2

9

this command chain works for your example:

sed -r 's/.*_([0-9]+) .*/\1 &/' file|sort -n|sed 's/[^ ]* //'

The idea is

  • extract the number first, add to the beginning of the line
  • sort all lines by this number
  • remove the number

update

sort by last number in the line, no matter where the number is:

awk -F'[^0-9]+' '{$0=(length($NF)?$NF:$(NF-1)) OFS $0}7' file|sort -n|sed 's/[^ ]* //'
5
  • Thanks! But I'm not very understand how it works. Does the .*_([0-9]+) .* matches _[a number]? And \1 &/ means copy it to the beginning? And I'm confused about the & , and how the last sed command work?
    – spring cc
    Jun 8, 2016 at 8:38
  • And, this is a simplified example. There may have lots of _[numbers], but I just want to match the last one. More general, there could be no _, the number just neighbored the char directly. And how could I do for these case? Just according to the last number of a line.
    – spring cc
    Jun 8, 2016 at 8:43
  • @springcc you could give a simple example, but it should represent all your requirement. So what you meant is, take the last number in each line as the key, no matter there is space/underscore or whatever?
    – Kent
    Jun 8, 2016 at 8:57
  • Sure, in this case I want to take the last number as the key. And I'm also curious about how to take the second number from the end as the key.
    – spring cc
    Jun 8, 2016 at 9:01
  • @springcc check the update in answer. It take the last number to sort, no matter which column it is in.
    – Kent
    Jun 8, 2016 at 11:11
7

If you want to do it with GNU awk, try this:

BEGIN { FS = "[ _]+" }
{ data[$(NF-1)] = data[$(NF-1)] "\n" $0}
END {
    n = asorti(data, sorted, "@val_num_asc");
    for (i = 1; i <= n; i++) {
        print substr(data[sorted[i]], 2);
    }
}

This works as follows: The BEGIN rule sets the field separator (you could also do this on the command line). The second rule applies to all lines of the input and puts them into an associative array indexed by the number in the second but last field. The END rule sorts the indices of this array into a second array, and the following loops prints the values, now sorted.

4
  • if multiple lines have same number, e.g. change the 3 in last line into 2 what your command will print? Awk doesn't solve hashtable key collision automatically.
    – Kent
    Jun 8, 2016 at 8:35
  • It will print the two lines in the correct order. The sort is stable. Jun 8, 2016 at 8:44
  • Ok, my bad, I didn't test. another thing, if you used asorti, you should set "sorted_in" in order to make it sort numerically.
    – Kent
    Jun 8, 2016 at 8:54
  • @MichaelVehrs Thank you, however, it's beyond my understanding, could you have a brief explain? I don't understand the functions such as BEGIN, END, asorti, sorted and the [ _]+$0 and how { data[$(NF-1)] = data[$(NF-1)] "\n" $0} works.
    – spring cc
    Jun 8, 2016 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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