0
点赞
收藏
分享

微信扫一扫

linux 命令:sort 详解

juneyale 2022-02-12 阅读 103

sort 命令的功能是对按行进行排序

用法:

sort [OPTION]... [FILE]...

sort [OPTION]... --files0-from=F

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

使用示例:

使用 du 命令查看 /usr/share下的文件,取前10个结果:

[root@server ~]# du -s /usr/share/* | head
12	/usr/share/aclocal
1332	/usr/share/alsa
648	/usr/share/anaconda
36	/usr/share/applications
8	/usr/share/augeas
440	/usr/share/authconfig
84	/usr/share/awk
18832	/usr/share/backgrounds
552	/usr/share/bash-completion
40	/usr/share/centos-logos

使用 sort 进行排序:

# 按数字倒序(从大到小)
[root@server ~]# du -s /usr/share/* | sort -nr | head
105112	/usr/share/locale
42252	/usr/share/doc
25600	/usr/share/vim
20292	/usr/share/perl5
18832	/usr/share/backgrounds
16232	/usr/share/man
9704	/usr/share/i18n
9196	/usr/share/cracklib
7236	/usr/share/hwdata
5544	/usr/share/mime

# 按数字正序(从小到大)
[root@server ~]# du -s /usr/share/* | sort -n | head
0	/usr/share/gcc-4.8.5
0	/usr/share/magic
0	/usr/share/redhat-release
4	/usr/share/desktop-directories
4	/usr/share/dict
4	/usr/share/empty
4	/usr/share/file
4	/usr/share/firewalld
4	/usr/share/games
4	/usr/share/gnome

使用 ll 打印 /usr/bin 下边的文件,取前 10 个结果,用sort 排序,用 k 选项指定第几个字段

[root@server ~]# ll /usr/bin/ | sort -nrk 1 | head
总用量 305060
---x--x--x  1 root root       57456 1月  27 2021 sudoreplay
---x--s--x  1 root nobody    382216 8月   9 2019 ssh-agent
---s--x--x  1 root root      147336 1月  27 2021 sudo
-r-xr-sr-x. 1 root tty        15344 6月  10 2014 wall
-rwx--s--x  1 root slocate    40520 4月  11 2018 locate
-rwxr-xr-x  2 root root      768648 4月  24 2019 x86_64-redhat-linux-gcc
-rwxr-xr-x  2 root root      768648 4月  24 2019 gcc
-rwxr-xr-x  2 root root       53329 1月  22 2019 s2p
-rwxr-xr-x  2 root root       53329 1月  22 2019 psed

[root@server ~]# ll /usr/bin/ | sort -nrk 2 | head
总用量 305060
-rwxr-xr-x  2 root root      768648 4月  24 2019 x86_64-redhat-linux-gcc
-rwxr-xr-x  2 root root      768648 4月  24 2019 gcc
-rwxr-xr-x  2 root root       53329 1月  22 2019 s2p
-rwxr-xr-x  2 root root       53329 1月  22 2019 psed
-rwxr-xr-x  2 root root       44351 1月  22 2019 perlthanks
-rwxr-xr-x  2 root root       44351 1月  22 2019 perlbug
-rwxr-xr-x  2 root root       36607 1月  22 2019 pstruct
-rwxr-xr-x  2 root root       36607 1月  22 2019 c2ph
-rwxr-xr-x  2 root root      185504 6月  10 2021 zipinfo

[root@server ~]# ll /usr/bin/ | sort -nrk 5 | head
-rwxr-xr-x  1 root root    19014016 12月 10 2020 mysqlpump
-rwxr-xr-x  1 root root    12972808 12月 10 2020 mysql_upgrade
-rwxr-xr-x  1 root root    11385136 12月 10 2020 mysqlbinlog
-rwxr-xr-x  1 root root    10743384 12月 10 2020 mysql
-rwxr-xr-x  1 root root    10295968 12月 10 2020 myisamchk
-rwxr-xr-x  1 root root    10194232 12月 10 2020 mysql_install_db
-rwxr-xr-x  1 root root     9907376 12月 10 2020 myisampack
-rwxr-xr-x  1 root root     9774584 12月 10 2020 myisam_ftdump
-rwxr-xr-x  1 root root     9767752 12月 10 2020 mysqldump
-rwxr-xr-x  1 root root     9691296 12月 10 2020 mysqlcheck

[root@server ~]# ll /usr/bin/ | sort -nrk 8 | head
---x--x--x  1 root root       57456 1月  27 2021 sudoreplay
---s--x--x  1 root root      147336 1月  27 2021 sudo
-rwxr-xr-x  2 root root      185504 6月  10 2021 zipinfo
-rwxr-xr-x  2 root root      185504 6月  10 2021 unzip
-rwxr-xr-x  1 root root       90768 6月  10 2021 unzipsfx
-rwxr-xr-x  1 root root       32128 6月  10 2021 funzip
-rwxr-xr-x  1 root root      247512 1月  27 2021 cvtsudoers
-rwxr-sr-x  1 root screen    475240 3月   9 2021 screen
lrwxrwxrwx  1 root root           9 3月   9 2021 python2 -> python2.7
lrwxrwxrwx  1 root root           8 3月   9 2021 systemd-loginctl -> loginctl

对 /etc/passwd 文件内容进行排序,设置分隔符为冒号,根据第三个字段进行排序:

[root@server ~]# sort -t ":" -k 3 /etc/passwd | head
root:x:0:0:root:/root:/bin/bash
test:x:1000:1000::/home/test:/bin/bash
aaa:x:1001:1001::/home/aaa:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false

编辑一个文本文件,展示下边的功能:

[root@server dir]# cat hahaha
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@server dir]# sort -k 1,1 -k 2n hahaha
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@server dir]# sort -k 3.7 -k 3.1 -k 3.4 hahaha
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
举报

相关推荐

0 条评论