asokan asokan - 2 months ago 7
Linux Question

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

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


Please help, thanks in advanvce

PS. PS.
Answer

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
Comments