sort not sorting as expected (space and locale)

前端 未结 2 1339
谎友^
谎友^ 2020-12-10 04:14

I want to sort a text file through linux sort, that looks like this

v 1006
v10 1
v 1011

I would expect result like this:

相关标签:
2条回答
  • 2020-12-10 04:46

    Your locale influences how the lines are sorted. For example I get this with my current locale:

    % echo -e "v 1006\nv10 1\nv 1011" | sort
    v 1006
    v10 1
    v 1011
    

    But with C locale I get this:

    % echo -e "v 1006\nv10 1\nv 1011" | LC_ALL=C sort
    v 1006
    v 1011
    v10 1
    

    I'm not sure why it behaves that way really. LC_ALL=C is pretty much equivalent to turning off all unexpected processing and going back to the byte-level operations (yeah - I'm skipping the details).

    Why do different locale settings skip space is harder to explain though. If anyone can explain that would be good :)

    0 讨论(0)
  • 2020-12-10 04:57

    It uses the system locale to determine the sorting order of letters. My guess is that with your locale, it ignores whitespace.

    $ cat foo.txt 
    v 1006
    v10 1
    v 1011
    $ LC_ALL=C sort foo.txt
    v 1006
    v 1011
    v10 1
    $ LC_ALL=en_US.utf8 sort foo.txt
    v 1006
    v10 1
    v 1011
    
    0 讨论(0)
提交回复
热议问题