sort
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 按月份排序 (其他 < 'JAN' < ... < 'DEC')
-h, --human-numeric-sort 按可读性更好的数字显示方式排序(1K,2G)
-n, --numeric-sort 按照字符串中的数字比较
-R, --random-sort 随机排序,但是将相同的键分组
--random-source=FILE 从 FILE 中获得随机字节
-r, --reverse 反向排序
--sort=WORD 根据 WORD 排序,可选项是:-g|h|M|n|R|V
-V, --version-sort 根据版本号数字排序
其他选项:
--batch-size=NMERGE 一次最多合并 NMERGE 输入,更多使用临时文件
-c, --check, --check=diagnose-first 检查是否排序
-C, --check=quiet, --check=silent 类似 -c, 不输出报错
--compress-program=PROG 使用 PROG 压缩,使用 PROG -d 解压缩
--debug 注释用于排序的行部分,并警告对 stderr 的可疑使用
--files0-from=F 从文件 F 中以空结尾的名称指定的文件中读取输入
-k, --key=KEYDEF 使用键排序,位置和类型由 KEYDEF 指定
-m, --merge 将排好序的文件合并
-o, --output=FILE 把排序结果输出到 FILE
-s, --stable 稳定比较
-S, --buffer-size=SIZE 使用 SIZE 作为主缓冲区大小
-t, --field-separator=SEP 指定 SEP 为分隔符
-T, --temporary-directory=DIR 使用 DIR 作为临时文件
--parallel=N 将同时运行的排序数设置为 N
-u, --unique 去重
-z, --zero-terminated 行分隔符是空,不是换行
--help 帮助文档
--version 版本信息
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
6.
[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
[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
- 使用 ll 打印 /opt 下边的文件,取前 10 个结果,用sort 排序,用 k 选项指定第几个字段
[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
- 对 /etc/passwd 文件内容进行排序,设置分隔符为:,根据第1个字段进行排序:
[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
Original: https://www.cnblogs.com/ivanlee717/p/16329733.html
Author: ivanlee717
Title: linux命令_sort
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/523469/
转载文章受原作者版权保护。转载请注明原作者出处!