POSIX “sort -n” alpha characters between zero and 1?
I'm having trouble getting POSIX "sort" to behave the way I want it to. When sorting numeric file names, those that start with letters show up between 0 and 1.
Why does this happen?
Is there a different sort option I can use to achieve the desired result?
$touch 0 1 a $ls | sort -n
Desired Output: 0 1 a
Actual Output: 0 a 1
In the gnu implementation, "-g" does roughly what I want, but that's not POSIX. Which unfortunately, means I can't use it.
If you want explicitly sort output as
- first numerically sorted files so 0, 1, 2, 11, 222, and after
- all other files sorted
you can do it for example with
cat <(ls -1 | grep '^[0-9]' |sort -n) <(ls -1 | grep -v '^[0-9]' | sort)
so catenate output of two commands
- sort numerically files what begins with numbers, and
- sort other files (what aren't begins with numbers)
but, this is works with bash only and don't know how much bash is POSIX, if bash is not ok you can use temporary files...
Its because you have the -n. That means numeric so a is being treated as 0. Get rid of the -n and you should be right (at the cost of 11 now coming before 2)