0

I have data like below, want to sort this value by numeric order. this command didn't work.

sort -t'_' -nk3,2 c

Input Data

5_5_1_2
5_5_1_3
5_5_1_4
5_5_1_5
5_5_2
5_5_3
5_5_4
5_5_5
5_6
5_7
6_1_10
6_1_11
6_1_12
6_1_13
6_1_14
6_1_2
6_1_3
6_1_4
6_1_5
6_1_6
6_1_7
6_1_8
6_1_9
6_2_10
6_2_11
6_2_12
6_2_13
6_2_14
6_2_15
6_2_16
6_2_17
6_2_1
6_2_2
6_2_3
1
  • 3
    Please add your desired output into the question.
    – polemon
    Commented Sep 23, 2021 at 2:39

2 Answers 2

5

It's kinda difficult to tell what you're trying to accomplish, but I'm assuming you need:

5_5_1_2
5_5_1_3
5_5_1_4
5_5_1_5
5_5_2
5_5_3
5_5_4
5_5_5
5_6
5_7
6_1_2
6_1_3
6_1_4
6_1_5
6_1_6
6_1_7
6_1_8
6_1_9
6_1_10
6_1_11
6_1_12
6_1_13
6_1_14
6_2_1
6_2_2
6_2_3
6_2_10
6_2_11
6_2_12
6_2_13
6_2_14
6_2_15
6_2_16
6_2_17

as output.

This can be obtained, by regarding the numbers as "versions", and "version sort" is available with the -V switch, like so:

sort -V <file>

This behavior is explained in the sort man page

1
  • 1
    May be worth noting that -V is a non-standard GNU extension not available in every sort implementation. Commented Sep 23, 2021 at 6:08
0

-kx,y means: use the section of the line that starts with the start of the xth field and ends at the end of the yth field as the sort key.

So -k3,2 does not make sense in that context. If you mean: sort numerically on the third fied, and then on the second field, that would be:

sort -nt_ -k3,3 -k2,2

Or:

sort -t_ -k3,3n -k2,2n

(here with the numeric flag applied to each key specification, as opposed to turning numeric sorting globally with the -n option).

1
  • Thanks, this worked for me sort -t_ -k1,1n -k2,2n -k3,3n
    – sfgroups
    Commented Sep 23, 2021 at 12:01

You must log in to answer this question.

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