Linux sort 命令用于将文本文件内容加以排序。
sort 可针对文本文件的内容,以行为单位来排序。sort可针对文本文件的内容,以行为单位来排序。sort命令将每一行作为一个单位进行比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按一定的顺序进行输出。sort命令经常可以与ls、cat等命令结合使用,将查询到的结果按照我们要求进行排序。
sort [-bcdfimMnr][-o][-t][+-][--help][--verison][文件][-k field1[,field2]]
选项:
排序类选项:
-b, --ignore-leading-blanks 忽略行首的空格
-d, --dictionary-order 只处理空格,英文字母和数字
-f, --ignore-case 小写字母视为大写字母
-g, --general-numeric-sort 按照数字大小比较
-i, --ignore-nonprinting 只考虑可打印的字符
-M, --month-sort 按月份排序 (其他
KEYDEF 是 F[.C][OPTS][,F[.C][OPTS]]
表示开始和停止位置,其中 F 是字段编号,C 是字段中的字符位置;两者都是从 1 开始,停止位置默认为行尾。如果 -t 和 -b 均无效,则字段中的字符从前一个空格的开头开始计数。OPTS 是一个或多个单字母排序选项 [bdfgiMhnRrV],它覆盖该键的全局排序选项。如果没有给出键,则使用整行作为键。使用 --debug 来诊断不正确的密钥使用。SIZE 可以加后缀:% b K M G T P E Z Y
[root@honey-master tmp] cat sort.txt
four
three
for
two
two
six
[root@honey-master tmp] sort sort.txt
for
four
six
three
two
two
[root@honey-master tmp] cat sort.txt
717
17
71
1
1
7
1.5
[root@honey-master tmp] sort sort.txt
1
1
1.5 浮点数也可以排序
17
7
71
717
[root@honey-master tmp] sort -u sort.txt
1
1.5
17
7
71
717
[root@honey-master tmp] sort -ur sort.txt
717
71
7
17
1.5
1
[root@honey-master tmp] du -sh * | sort -hr
96K hsperfdata_root
92K chromium-ym2SRD
88K chromium-6OMI0k
32K systemd-private-5a76db78a7624ffe8c521b35a8f39be6-elasticsearch-0.service-MRQ20O
4.0K sort.txt
0 pymp-yl2enfzo
[root@honey-master tmp] du -s /usr/share/* | head
732 /usr/share/aclocal
156 /usr/share/aclocal-1.16
18712 /usr/share/adobe
360 /usr/share/alsa
96 /usr/share/anaconda
100 /usr/share/appdata
56 /usr/share/applications
380 /usr/share/asciidoc
160 /usr/share/audit
1336 /usr/share/augeas
这里的管head默认输出前10行的数据,后面也可以加参数进行改变,比如
使用sort排序之后:
[root@honey-master tmp] du -s /usr/share/* | head | sort -nr
18712 /usr/share/adobe
1336 /usr/share/augeas
732 /usr/share/aclocal
380 /usr/share/asciidoc
360 /usr/share/alsa
160 /usr/share/audit
156 /usr/share/aclocal-1.16
100 /usr/share/appdata
96 /usr/share/anaconda
56 /usr/share/applications
[root@honey-master opt] ll | sort -nk 1| head
-r--r--r--. 1 root root 166062080 May 30 22:06 ffmpeg-git-amd64-static.tar
-rw-r--r--. 1 root root 70159813 May 30 22:13 kafka_2.11-2.4.1.tgz
-rw-r--r--. 1 root root 630905856 May 30 22:09 asciicast2mp4.tar
-rw-r--r--. 1 root root 777374720 May 30 22:09 singlefile.tar.gz
-rw-r--r--. 1 root root 874286592 May 30 22:08 logstash.tar.gz
-rwxr-xr-x. 1 root root 56806961 May 30 22:12 cerebro-0.9.2.tgz
drwx--x--x. 4 root root 28 May 30 22:09 containerd
drwxr-xr-x. 2 root root 29 May 30 22:32 nta
drwxr-xr-x. 2 root root 62 May 30 22:46 msf
drwxr-xr-x. 2 root root 87 May 30 22:13 kafka.cli.test
[root@honey-master opt] ll | sort -nk 2| head
-r--r--r--. 1 root root 166062080 May 30 22:06 ffmpeg-git-amd64-static.tar
-rw-r--r--. 1 root root 70159813 May 30 22:13 kafka_2.11-2.4.1.tgz
-rw-r--r--. 1 root root 630905856 May 30 22:09 asciicast2mp4.tar
-rw-r--r--. 1 root root 777374720 May 30 22:09 singlefile.tar.gz
-rw-r--r--. 1 root root 874286592 May 30 22:08 logstash.tar.gz
-rwxr-xr-x. 1 root root 56806961 May 30 22:12 cerebro-0.9.2.tgz
drwxr-xr-x. 2 root root 29 May 30 22:32 nta
drwxr-xr-x. 2 root root 62 May 30 22:46 msf
drwxr-xr-x. 2 root root 87 May 30 22:13 kafka.cli.test
drwxr-xr-x. 2 root root 88 May 30 22:46 pvp-strike
[root@honey-master opt] ll | sort -nk 5| head
total 2515244
drwxr-xr-x. 3 root root 17 May 30 22:07 python2.7.10-customized
drwxr-xr-x. 3 root root 17 May 30 22:07 python3.6.10-customized
drwxr-xr-x. 3 root root 17 Sep 18 2020 redis
drwxr-xr-x. 3 root root 18 May 30 22:08 honeypot-web
drwx--x--x. 4 root root 28 May 30 22:09 containerd
drwxr-xr-x. 2 root root 29 May 30 22:32 nta
drwxr-xr-x. 5 501 games 57 Jun 18 2020 cerebro-0.9.2
drwxr-xr-x. 2 root root 62 May 30 22:46 msf
drwxr-xr-x. 6 root root 70 May 30 22:13 kafka-manager-1.3.3.23
[root@honey-master opt] ll | sort -nk 7| head
total 2515244
drwxr-xr-x. 3 root root 17 Sep 18 2020 redis
drwxr-xr-x. 5 501 games 57 Jun 18 2020 cerebro-0.9.2
drwxr-xr-x. 10 postgres postgres 4096 Feb 20 2014 zookeeper
-r--r--r--. 1 root root 166062080 May 30 22:06 ffmpeg-git-amd64-static.tar
-rw-r--r--. 1 root root 70159813 May 30 22:13 kafka_2.11-2.4.1.tgz
-rw-r--r--. 1 root root 630905856 May 30 22:09 asciicast2mp4.tar
-rw-r--r--. 1 root root 777374720 May 30 22:09 singlefile.tar.gz
-rw-r--r--. 1 root root 874286592 May 30 22:08 logstash.tar.gz
-rwxr-xr-x. 1 root root 56806961 May 30 22:12 cerebro-0.9.2.tgz
[root@honey-master opt] cat /etc/passwd | sort -t ":" -k 1 | head
adm:x:3:4:adm:/var/adm:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
chrony:x:990:986::/var/lib/chrony:/sbin/nologin
clamscan:x:986:981:Clamav scanner user:/:/sbin/nologin
clamupdate:x:987:983:Clamav database update user:/var/lib/clamav:/sbin/nologin
clevis:x:995:991:Clevis Decryption Framework unprivileged user:/var/cache/clevis:/sbin/nologin
cockpit-ws:x:993:989:User for cockpit web service:/nonexisting:/sbin/nologin
cockpit-wsinstance:x:992:988:User for cockpit-ws instances:/nonexisting:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@honey-master opt] sort -t ":" -k 3 -nr /etc/passwd | head > /tmp/out.log
[root@honey-master opt] cat /tmp/out.log
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
postgres:x:1000:1000::/home/postgres:/bin/bash
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
unbound:x:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin
libstoragemgmt:x:996:994:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
clevis:x:995:991:Clevis Decryption Framework unprivileged user:/var/cache/clevis:/sbin/nologin
setroubleshoot:x:994:990::/var/lib/setroubleshoot:/sbin/nologin
cockpit-ws:x:993:989:User for cockpit web service:/nonexisting:/sbin/nologin
cockpit-wsinstance:x:992:988:User for cockpit-ws instances:/nonexisting:/sbin/nologin
-o 参数:这个不支持部分排序操作,需要将所有排序之后写入一个文件,也可以是源文件,不如 > 灵活
[root@honey-master tmp] sort -t ":" -k 3 -nr passwd -o passwd.txt
[root@honey-master tmp] ls
passwd.txt passwd
[root@honey-master tmp] cat test
Alice 98 02/20/2021
Alice 97 01/31/2021
Alice 92 03/01/2020
Alice 95 07/11/2020
Bob 99 12/23/2021
Bob 91 02/25/2021
Bob 98 01/30/2020
Chris 98 02/19/2021
Chris 98 02/05/2020
Chris 94 02/08/2021
Chris 92 02/10/2021
Chris 93 02/13/2021
指定先以第1个字段排序,再以第2个字段,且指定类型为数字:
[root@honey-master tmp] sort -k 1 -k 2n test
Alice 92 03/01/2020
Alice 95 07/11/2020
Alice 97 01/31/2021
Alice 98 02/20/2021
Bob 91 02/25/2021
Bob 98 01/30/2020
Bob 99 12/23/2021
Chris 92 02/10/2021
Chris 93 02/13/2021
Chris 94 02/08/2021
Chris 98 02/05/2020
Chris 98 02/19/2021
指定按顺序以第3个字段的第7个、第1个、第4个字符排序:是按照年份的第一位,月份的第一位,以及日期的第一位排序
[root@honey-master tmp] sort -k 3.7 -k 3.1 -k 3.4 test
Bob 98 01/30/2020
Chris 98 02/05/2020
Alice 92 03/01/2020
Alice 95 07/11/2020
Alice 97 01/31/2021
Chris 94 02/08/2021
Chris 92 02/10/2021
Chris 93 02/13/2021
Chris 98 02/19/2021
Alice 98 02/20/2021
Bob 91 02/25/2021
Bob 99 12/23/2021
-k M,N :M的意思是,从哪个域开始作为比较的依据;N截止到哪个域比较结束。M,N是一个(域的)范围,如果这个M,N结合-u使用。那么-u的比较范围就是M域和N域之间的这段内容。如果数字N被省略,那么-u比较的范围就是从M域开始,一直到每行的最后一个域的最后一个字符。
首先说-u的作用是整行都完全重合,才会去重,其他时候不会去重
单列名字并不会去重
如果配合上-k参数,-t参数默认为" ",且-k只有一个参数的时候,默认作用域是从第一个参数到末尾,所以后面的不重复,这一行也不会去重
但如果参数有两个,比如-k 1,1
,那么作用域就只限于第一个部分,此时就可以去重了
[root@honey-master tmp] sort -u -k 1 test
Alice 92 03/01/2020
Alice 95 07/11/2020
Alice 97 01/31/2021
Alice 98 02/20/2021
Bob 91 02/25/2021
Bob 98 01/30/2020
Bob 99 12/23/2021
Chris 92 02/10/2021
Chris 93 02/13/2021
Chris 94 02/08/2021
Chris 98 02/05/2020
Chris 98 02/19/2021
[root@honey-master tmp] sort -u -k 1,1 test
Alice 98 02/20/2021
Bob 99 12/23/2021
Chris 98 02/19/2021
本文来自博客园,作者:ivanlee717,转载请注明原文链接:https://www.cnblogs.com/ivanlee717/p/16329733.html
参与评论
手机查看
返回顶部