8

I would like to sort this numerically and the sort command keys off the first character.

CPL_V11.01.00_1
CPL_V11.01.00_10
CPL_V11.01.00_2
CPL_V11.01.00_3
CPL_V11.01.00_35
CPL_V11.01.00_36
CPL_V11.01.00_37
CPL_V11.01.00_38
CPL_V11.01.00_39
CPL_V11.01.00_4
CPL_V11.01.00_40
CPL_V11.01.00_41
CPL_V11.01.00_42
CPL_V11.01.00_43
CPL_V11.01.00_44
CPL_V11.01.00_45
CPL_V11.01.00_46
CPL_V11.01.00_47

What should I do here?

Bill
  • 81

1 Answers1

11
sort -V input

from man sort:

   -V, --version-sort
       natural sort of (version) numbers within text

that will get you:

CPL_V11.01.00_1
CPL_V11.01.00_2
CPL_V11.01.00_3
CPL_V11.01.00_4
CPL_V11.01.00_10
CPL_V11.01.00_35
CPL_V11.01.00_36
CPL_V11.01.00_37
CPL_V11.01.00_38
CPL_V11.01.00_39
CPL_V11.01.00_40
CPL_V11.01.00_41
CPL_V11.01.00_42
CPL_V11.01.00_43
CPL_V11.01.00_44
CPL_V11.01.00_45
CPL_V11.01.00_46

You can also use:

sort -t _ -k 3 -n input

(split on _ and use the 3rd field sorted by number)

Anthon
  • 79,293