Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort numerically in unix/ linux where "sort -V" not available [duplicate]

I am want to sort below shown lines in a file numerically. it is not coming as expected.

$ cat test
abc1
abc10
abc2
abc11
abc20
abcd1
abcd10
abcd2
abcd11
abcd20
$

Output:-

$ sort test
abc1
abc10
abc11
abc2
abc20
abcd1
abcd10
abcd11
abcd2
abcd20
$

Output Expected:-

abc1
abc2
abc10
abc11
abc20
abcd1
abcd2
abcd10
abcd11
abcd20
like image 770
asokan Avatar asked Feb 07 '23 03:02

asokan


1 Answers

Use sort -V for alphanumeric sorting where numbers are part of string.

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

sort -V test
abc1
abc2
abc10
abc11
abc20
abcd1
abcd2
abcd10
abcd11
abcd20
like image 99
P.... Avatar answered Feb 08 '23 16:02

P....