5

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?

  • sorrry for the missing CR/LF – Bill Nov 22 '14 at 22:57
8
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)

  • am using cygwin and sort -t _ -k 3 -n input works best – Bill Nov 23 '14 at 2:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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