V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
aliipay
V2EX  ›  Linux

请教一个 sort 命令的问题

  •  
  •   aliipay · 2018-04-17 20:29:34 +08:00 · 3855 次点击
    这是一个创建于 2172 天前的主题,其中的信息可能已经有所发展或是发生改变。
    原始文件:
    # cat 2.txt
    r0:26
    r1:26
    r10:26
    r11:26
    r12:26
    r13:26
    r14:26

    排序结果:
    # cat 2.txt | sort -k1 -t ':'
    r0:26
    r10:26
    r11:26
    r12:26
    r1:26
    r13:26
    r14:26

    ------------

    r1:26 为何在 5 行? 直接 sort 和 sort -g 结果都一样,中间冒号是被忽略的?
    第 1 条附言  ·  2018-04-18 01:33:01 +08:00
    感谢#9 #10 提供的方法。
    已经搞清楚问题了,谢谢大家~~
    14 条回复    2018-04-18 01:36:17 +08:00
    rwdy2008
        1
    rwdy2008  
       2018-04-17 20:36:00 +08:00 via iPhone
    看下异常行这个冒号是不是中文格式了
    aliipay
        2
    aliipay  
    OP
       2018-04-17 20:39:06 +08:00
    @rwdy2008 你复制粘贴试试就知道了,没什么卵用,再说如果是中文冒号插在中间是不是也很奇怪
    MeteorCat
        3
    MeteorCat  
       2018-04-17 20:43:02 +08:00 via Android
    是不是把 r 的 ascii 码也计算在里面变成 r 的 ascii+0 的 ascii 位数排序,试着剔除 r 开头看看
    bin456789
        4
    bin456789  
       2018-04-17 20:49:42 +08:00   ❤️ 1
    sed 's/^r//' 2.txt | sort -k1n -t ':' | sed 's/^/r/'

    勉强能用
    ant2017
        5
    ant2017  
       2018-04-17 20:54:14 +08:00   ❤️ 1
    cat 2.txt|sort -k 1.2n -t :
    jasonyang9
        6
    jasonyang9  
       2018-04-17 20:56:40 +08:00
    KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a field number and C a character position in the field; both are origin 1, and the stop position defaults to the line's end. If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace. OPTS is one or more single-letter ordering options [bdfgiMhnRrV], which over ‐ ride global ordering options for that key. If no key is given, use the entire line as the key.

    `man sort`中的这段看不懂,哪位能帮忙解释下并举几个例子??
    aliipay
        7
    aliipay  
    OP
       2018-04-17 21:05:57 +08:00
    @bin456789
    r 和冒号之间如果不是全数字你这个就无法跑了
    aliipay
        8
    aliipay  
    OP
       2018-04-17 21:07:04 +08:00
    @ant2017
    @bin456789
    @MeteorCat
    我的问题是 r1:26 为何在 5 行? 谢谢各位
    goreliu
        9
    goreliu  
       2018-04-17 21:29:58 +08:00   ❤️ 1
    把 LANG 设置成 C,不然 sort 会出现各种奇怪现象。

    % cat 2.txt | LANG=C sort -k1 -t ':'
    r0:26
    r10:26
    r11:26
    r12:26
    r13:26
    r14:26
    r1:26
    Lpl
        10
    Lpl  
       2018-04-17 22:52:27 +08:00   ❤️ 1
    https://stackoverflow.com/questions/6531674/linux-sort-unexpected-output?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

    这个回复说的挺好的,其实你的数据
    `
    r0:26 -> r026
    r10:26 -> r1026
    r11:26 -> r1226
    r12:26 -> ....
    r1:26
    r13:26
    r14:26
    `
    特殊字符会被忽略,然后按照字典序来进行排序
    goodryb
        11
    goodryb  
       2018-04-17 23:41:22 +08:00
    $cat 2.txt ;echo "----------" && cat 2.txt | sort -k1 -t ':'
    r0:26
    r1:26
    r10:26
    r11:26
    r12:26
    r13:26
    r14:26
    ----------
    r0:26
    r10:26
    r11:26
    r12:26
    r13:26
    r14:26
    r1:26


    为啥我执行的结果和楼主不一样呢
    iwishing
        12
    iwishing  
       2018-04-17 23:52:15 +08:00
    字典顺序
    r1226
    r126
    r1326
    guoer
        13
    guoer  
       2018-04-18 00:25:39 +08:00
    少了个-n ?
    aliipay
        14
    aliipay  
    OP
       2018-04-18 01:36:17 +08:00
    @goodryb 按#9 #10 的方法,考虑是否已经默认设置了 LANG 或者 LC_ALL,我的 LANG 默认是 en_US.UTF-8
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5880 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 02:19 · PVG 10:19 · LAX 19:19 · JFK 22:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.