开发者

linux sort unexpected output

开发者 https://www.devze.com 2023-03-16 04:25 出处:网络
I use sort file ABC AB-C ABCDEFG-HI I get ABC 开发者_StackOverflow社区AB-C ABCDEFG-HI why does sort orders the string this way?how do I make it sort \'-\' alphabetically?The solution provided

I use sort file

ABC
AB-C
ABCDEFG-HI

I get

ABC
开发者_StackOverflow社区AB-C
ABCDEFG-HI

why does sort orders the string this way? how do I make it sort '-' alphabetically?


The solution provided by @cnicutar is correct, but the reason needs explanation which is why I'm giving a new answer.

After the discussion with @cnicutar where in the end I suspected a bug in coreutils' sort I found that this sorting behavior is expected:

At that point sort appears broken because case is folded and punctuation is ignored because ‘en_US.UTF-8’ specifies this behavior.

So to sort, your input seems to be mapped as follows:

ABC -> ABC
AB-C -> ABC
ABCDEFG-HI -> ABCDEFGHI

If you want pure ASCII sorting, you need to call LC_ALL=C sort (temporarily set the locale to C when calling sort which means "standard" behavior without localization; you can also use POSIX instead of C).

On other Unixes this behavior seems to be different (tested on Mac OS X which userland tools are derived from FreeBSD), but LC_ALL=C sort should yield the same behavior across all POSIX systems.


I remember this :)) try

[cnicutar@aiur ~]$ LANG=POSIX sort
ABC
AB-C
ABCDEFG-HI
AB-C
ABC
ABCDEFG-HI

Alternatively LANG=C should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号