小强哥博客

小强哥,小强哥博客,技术大咖

几种常用的sort命令

参  数:
  -b   忽略每行前面开始出的空格字符。
  -c   检查文件是否已经按照顺序排序。
  -d   排序时,处理英文字母、数字及空格字符外,忽略其他的字符。
  -f   排序时,将小写字母视为大写字母。
  -i   排序时,除了040至176之间的ASCII字符外,忽略其他的字符。
  -m   将几个排序好的文件进行合并。
  -M   将前面3个字母依照月份的缩写进行排序。
  -n   依照数值的大小排序。
  -o<输出文件>   将排序后的结果存入指定的文件。
  -r   以相反的顺序来排序。
  -t<分隔字符>   指定排序时所用的栏位分隔字符。
  +<起始栏位>-<结束栏位>   以指定的栏位来排序,范围由起始栏位到结束栏位的前一栏位。
  --help   显示帮助。
  --version   显示版本信息

如下数据,

yellow open logstash-box-2018.03.08                        ECdL8p4NQamqZ5KiH5HeEg 5 1    2419 0    1.9mb    1.9mb
yellow open logstash-box-2018.04.07                        rGrqEfjRSC-0B-Qq7DVd6Q 5 1     288 0  542.4kb  542.4kb
yellow open logstash-box-2018.02.01                        JIFFsxFnQCuXCkrdFLGLzg 5 1  233662 0  116.2mb  116.2mb
yellow open logstash-box-2018.04.28                        Ydsgt1ZKRRSXb0vyLzB9cg 5 1     489 0  843.4kb  843.4kb
yellow open logstash-box-2018.04.19                        OFRzqeR6SeO9CKTCjdQNpQ 5 1     554 0  773.2kb  773.2kb
yellow open logstash-box-2018.02.13                        8FacNYC9TtOiRgijp2j4Xg 5 1     288 0  542.4kb  542.4kb
yellow open logstash-box-2018.04.06                        q7cta_OwRcSdgl8KdDiV3g 5 1     288 0    542kb    542kb
yellow open logstash-box-2018.01.26                        h_mXtL_cSWazymWuuP5J_A 5 1     186 0  533.7kb  533.7kb
yellow open logstash-box-2018.02.28                        TaDCqH-BRe-Ete3Zv7AwTg 5 1   26376 0    6.5mb    6.5mb
yellow open logstash-box-2018.04.29                        hENwOG2_QuiC_YawVqaV6A 5 1     304 0  415.8kb  415.8kb
yellow open logstash-box-2018.02.07                        x-ssXqHkSj-sHK299LkN0Q 5 1    1430 0    1.2mb    1.2mb
yellow open logstash-box-2018.02.02                        S5K0nCY4QYekSmtV37CBQA 5 1   33461 0   19.7mb   19.7mb
yellow open logstash-box-2018.03.23                        e8tMvn78Sxm4gQPoW00M2g 5 1     472 0  670.6kb  670.6kb
yellow open logstash-box-2018.02.12                        MuYIJqj5S7qMKQNEk3U5GQ 5 1     288 0  541.8kb  541.8kb
yellow open logstash-box-2018.03.02                        4NmnBwn6QlKJ4v0W2Ixzug 5 1     110 0  202.2kb  202.2kb

现在想对第三列升序排序,

[root@essdev-shqs-2 ~]#  curl http://10.99.70.54:9200/_cat/indices  |grep logstash-box | sort -k 3
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 77178  100 77178    0     0  34102      0  0:00:02  0:00:02 --:--:-- 34119
yellow open logstash-box-2018.01.24                        BnvWnf3uTMqQdDFHQfuZHA 5 1     520 0  759.6kb  759.6kb
yellow open logstash-box-2018.01.25                        B2Cme7e6RvG4bY9AvodDEw 5 1     738 0  862.2kb  862.2kb
yellow open logstash-box-2018.01.26                        h_mXtL_cSWazymWuuP5J_A 5 1     186 0  533.7kb  533.7kb
yellow open logstash-box-2018.01.29                        GHV_VXJ-R3i1Sp7l9LXVRA 5 1      13 0   49.9kb   49.9kb
yellow open logstash-box-2018.01.30                        RyEeJFovQSiyspt94T1veg 5 1     105 0  471.9kb  471.9kb
yellow open logstash-box-2018.01.31                        NsgUDUKZT4yj8i4feDQEbA 5 1    1935 0    1.7mb    1.7mb
yellow open logstash-box-2018.02.01                        JIFFsxFnQCuXCkrdFLGLzg 5 1  233662 0  116.2mb  116.2mb
yellow open logstash-box-2018.02.02                        S5K0nCY4QYekSmtV37CBQA 5 1   33461 0   19.7mb   19.7mb
yellow open logstash-box-2018.02.05                        -ZBhfwHtT5m21pbdspFFRA 5 1   31402 0   18.9mb   18.9mb
yellow open logstash-box-2018.02.07                        x-ssXqHkSj-sHK299LkN0Q 5 1    1430 0    1.2mb    1.2mb
yellow open logstash-box-2018.02.08                        4p8XbJ-xR1SUPdhq8aTQuA 5 1     720 0  810.2kb  810.2kb
yellow open logstash-box-2018.02.09                        lMppJAriRn6n-d3xrmCcVQ 5 1     288 0  542.6kb  542.6kb
yellow open logstash-box-2018.02.10                        L07nDKp0QFef94x4lKOwfA 5 1     288 0  446.7kb  446.7kb
yellow open logstash-box-2018.02.11                        arddpGlrSlqSFsD8CzGWdw 5 1     304 0  630.3kb  630.3kb
yellow open logstash-box-2018.02.12                        MuYIJqj5S7qMKQNEk3U5GQ 5 1     288 0  541.8kb  541.8kb
yellow open logstash-box-2018.02.13                        8FacNYC9TtOiRgijp2j4Xg 5 1     288 0  542.4kb  542.4kb

通过-r参数可以对顺序反转,

[root@essdev-shqs-2 ~]#  curl http://10.99.70.54:9200/_cat/indices  |grep logstash-box | sort -r -k 3
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 77178  100 77178    0     0   224k      0 --:--:-- --:--:-- --:--:--  224k
yellow open logstash-box-2018.05.10                        J-O9AGUFQT6bdD5DCIBeRg 5 1     607 0  727.3kb  727.3kb
yellow open logstash-box-2018.05.09                        3rwCOlcyRtqZY8K38YV2dg 5 1     547 0    791kb    791kb
yellow open logstash-box-2018.05.08                        _uZOT8JwTRyg1XWUE-ciUg 5 1     564 0  773.4kb  773.4kb
yellow open logstash-box-2018.05.07                        42JZ49skRKe_EVuVxYCllw 5 1    1339 0    1.1mb    1.1mb
yellow open logstash-box-2018.05.06                        XoBU7-T-SvyyE9exCwp-Fw 5 1     288 0  638.8kb  638.8kb
yellow open logstash-box-2018.05.05                        x54I8Ja4TECasgegBDN4eQ 5 1     301 0  601.1kb  601.1kb
yellow open logstash-box-2018.05.04                        HcXQmU8QSqWpeMy3pxTpkQ 5 1     527 0  614.3kb  614.3kb
yellow open logstash-box-2018.05.03                        HRi97mKvScmhS-QuKTpuBw 5 1    1591 0    1.1mb    1.1mb
yellow open logstash-box-2018.05.02                        ZigDSn5dT--qBp3I61A-Kg 5 1     288 0  542.4kb  542.4kb

对第7列数字进行排序,

yellow open logstash-box-2018.05.07                        42JZ49skRKe_EVuVxYCllw 5 1    1339 0    1.1mb    1.1mb
yellow open logstash-box-2018.02.07                        x-ssXqHkSj-sHK299LkN0Q 5 1    1430 0    1.2mb    1.2mb
yellow open logstash-box-2018.03.31                        WTf6gYK7SBmQ6QpN6jpVcQ 5 1    1521 0    1.2mb    1.2mb
yellow open logstash-box-2018.04.10                        gPxSRSvaQ72W8CBntb9YMQ 5 1    1573 0    1.1mb    1.1mb
yellow open logstash-box-2018.05.03                        HRi97mKvScmhS-QuKTpuBw 5 1    1591 0    1.1mb    1.1mb
yellow open logstash-box-2018.03.15                        DEL7wBhQQCSgduNwY6ynDQ 5 1    1733 0    1.4mb    1.4mb
yellow open logstash-box-2018.01.31                        NsgUDUKZT4yj8i4feDQEbA 5 1    1935 0    1.7mb    1.7mb
yellow open logstash-box-2018.03.08                        ECdL8p4NQamqZ5KiH5HeEg 5 1    2419 0    1.9mb    1.9mb
yellow open logstash-box-2018.03.05                        sfl3KppaQBOrI9gZkHHanQ 5 1    2880 0    1.5mb    1.5mb
yellow open logstash-box-2018.02.28                        TaDCqH-BRe-Ete3Zv7AwTg 5 1   26376 0    6.5mb    6.5mb
yellow open logstash-box-2018.02.05                        -ZBhfwHtT5m21pbdspFFRA 5 1   31402 0   18.9mb   18.9mb
yellow open logstash-box-2018.02.02                        S5K0nCY4QYekSmtV37CBQA 5 1   33461 0   19.7mb   19.7mb
yellow open logstash-box-2018.02.26                        wyY1wfNuRSWr5rp7l7c24A 5 1  163627 0   35.5mb   35.5mb
yellow open logstash-box-2018.02.01                        JIFFsxFnQCuXCkrdFLGLzg 5 1  233662 0  116.2mb  116.2mb
yellow open logstash-box-2018.02.27                        X-gK2cIESJmyxCx9_jAybQ 5 1  247887 0     54mb     54mb
yellow open logstash-box-2018.04.26                        iBchGczoRI6Q_JUs_ubEqQ 5 1  568226 0  227.2mb  227.2mb


其它sort命令可以参考https://www.cnblogs.com/fulucky/p/8022718.html