#Bash 的 sort 命令
sort [OPTION]... [FILE]...
功能
将文本文件的行进行排序。
类型
可执行文件(/usr/bin/sort),属于 coreutils。
参数
OPTION选项:-b,--ignore-leading-blanks- 忽略开头的空白-d,--dictionary-order- 使用字典序,忽略字母、数字、空格以外的字符(默认)-f,--ignore-case- 忽略大小写,小写字母当作大写字母-g,--general-numeric-sort- 比较广义数值大小('NaN'<'-inf'<'2'<'3.14'<'10'<'1e4'<'inf')-i,--ignore-nonprinting- 忽略不可打印的字符(参考 isprint)-M,--month-sort- 比较月份大小(其它 <'JAN'<'FEB'< ... <'DEC')-h,--human-numeric-sort- 比较含单位的数值大小(2K<1G)-n,--numeric-sort- 比较数值大小('2'<'3.14'<'10'),非数值视为 0-R,--random-sort- 随机排序(但是相同的行会在一起)--random-source=FILE- 通过FILE获取随机数-r,--reverse- 逆序输出;默认为升序,此选项为降序--sort=WORD- 按WORD排序:general-numeric- 同-g,比较广义数值大小('NaN'<'-inf'<'2'<'3.14'<'10'<'1e4'<'inf')human-numeric- 同-h,比较含单位的数值大小(2K<1G)month- 同-M,比较月份大小(其它 <'JAN'<'FEB'< ... <'DEC')numeric- 同-n,比较数值大小('2'<'3.14'<'10'),非数值视为 0random- 同-R,随机排序(但是相同的行会在一起)version- 同-V,比较版本号大小('v3.2'<'v10.1')
-V,--version-sort- 比较版本号大小('v3.2'<'v10.1')--batch-size=NMERGE- 一个合并最多NMERGE个输入-c,--check,--check=diagnose-first- 检查输入是否已经排序,而不是进行排序-C,--check=quiet,--check=silent- 检查输入是否已经排序,而不是进行排序,并且不报告错序行--compress-program=PROG- 使用PROG命令临时进行压缩,之后使用PROG -d命令解压--debug- 打印调试信息;对用于排序的行进行注释并将可疑用法输出到标准错误--files0-from=F- 从文件F中读取文件列表作为输入,每个文件名以空字符(\0)结尾;如果F是-则从标准输入读取文件列表-k,--key=KEYDEF- 根据键进行排序-m,--merge- 合并已经排序的文件,而不是进行排序-o,--output=FILE- 将结果输出到FILE文件;默认为标准输出-s,--stable- 稳定排序-S,--buffer-size=SIZE- 使用SIZE主内存缓冲-t,--field-separator=SEP- 使用SEP作为分隔,而不是非空白到空白的过渡作为分隔-T,--temporary-directory=DIR- 使用DIR作为临时目录,而不是$TMPDIR或/tmp--parallel=N- 并发运行数量设为N-u,--unique- 排序时相同的行只输出一次;检查排序(与-c一同使用)时严格检查顺序 (不能有重复行)-z,--zero-terminated- 以空字符(\0)作为行的结尾,而不是换行符(\n)--help- 显示帮助--version- 显示版本
FILE- 文件列表;如果没有这个参数或指定为-,则读取标准输入
#示例
示例文件内容:
apple
Apple
!banana
10
2
50K
3M
Jan
Feb
v1
v2
v10
3.5
1e2
NaN
默认排序(字典序)
$ sort 1.txt
10
1e2
2
3.5
3M
50K
apple
Apple
!banana
Feb
Jan
NaN
v1
v10
v2
按数值排序
$ sort -n 1.txt
apple
Apple
!banana
Feb
Jan
NaN
v1
v10
v2
1e2
2
3M
3.5
10
50K
按广义数值排序
$ sort -g 1.txt
apple
Apple
!banana
Feb
Jan
v1
v10
v2
NaN
2
3M
3.5
10
50K
1e2
含单位的数值排序
$ sort -h 1.txt
apple
Apple
!banana
Feb
Jan
NaN
v1
v10
v2
1e2
2
3.5
10
50K
3M
$ du -d1 -h ~/Downloads/ | sort -h # 查看 ~/Downloads/ 中一级子目录的大小,按大小排序
8.0K ~/Downloads/logs
2.5M ~/Downloads/byte-unixbench
2.5M ~/Downloads/demo
24M ~/Downloads/versions
42M ~/Downloads/libraries
344M ~/Downloads/jdk-23.0.2
389M ~/Downloads/jdk-26.0.1
571M ~/Downloads/QtTheme
1.4G ~/Downloads/
版本号排序
$ sort -V 1.txt
1e2
2
3M
3.5
10
50K
Apple
Feb
Jan
NaN
apple
v1
v2
v10
!banana
月份排序
$ sort -M 1.txt
10
1e2
2
3.5
3M
50K
apple
Apple
!banana
NaN
v1
v10
v2
Jan
Feb
合并文件排序
$ cat core-tuils.md util-linux.md | sort
| [addpart](/document/zh/10.Bash/90.帮助手册/EX.addpart.md) | 添加硬盘分区 |
| [agetty](/document/zh/10.Bash/90.帮助手册/EX.agetty.md) | 在终端设备上启动登录提示并等待用户登录 |
| [arch](/document/zh/10.Bash/90.帮助手册/EX.arch.md) | 查看硬件架构 |
| [b2sum](/document/zh/10.Bash/90.帮助手册/EX.b2sum.md) | 计算、校验 BLAKE2 校验和 |
| [base32](/document/zh/10.Bash/90.帮助手册/EX.base32.md) | 进行 BASE32 编码或解码 |
| [base64](/document/zh/10.Bash/90.帮助手册/EX.base64.md) | 进行 BASE64 编码或解码 |
| [basename](/document/zh/10.Bash/90.帮助手册/EX.basename.md) | 提取文件名(去除目录和后缀) |
| [basenc](/document/zh/10.Bash/90.帮助手册/EX.basenc.md) | 对数据进行编码或解码 |
| [blkdiscard](/document/zh/10.Bash/90.帮助手册/EX.blkdiscard.md) | 丢弃磁盘设备的区块 |
| [blkid](/document/zh/10.Bash/90.帮助手册/EX.blkid.md) | 获取块设备的属性 |
| [blkzone](/document/zh/10.Bash/90.帮助手册/EX.blkzone.md) | 管理和查看分区式(zoned)块设备的 zone 信息 |
| [blockdev](/document/zh/10.Bash/90.帮助手册/EX.blockdev.md) | 从命令行对块设备调用 `ioctl` |
#推荐阅读
#手册
SORT(1) User Commands SORT(1)
NAME
sort - sort lines of text files
SYNOPSIS
sort [OPTION]... [FILE]...
sort [OPTION]... --files0-from=F
DESCRIPTION
Write sorted concatenation of all FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options
too. Ordering options:
-b, --ignore-leading-blanks
ignore leading blanks
-d, --dictionary-order
consider only blanks and alphanumeric characters
-f, --ignore-case
fold lower case to upper case characters
-g, --general-numeric-sort
compare according to general numerical value
-i, --ignore-nonprinting
consider only printable characters
-M, --month-sort
compare (unknown) < 'JAN' < ... < 'DEC'
-h, --human-numeric-sort
compare human readable numbers (e.g., 2K 1G)
-n, --numeric-sort
compare according to string numerical value
-R, --random-sort
shuffle, but group identical keys. See shuf(1)
--random-source=FILE
get random bytes from FILE
-r, --reverse
reverse the result of comparisons
--sort=WORD
sort according to WORD: general-numeric -g, human-numeric -h,
month -M, numeric -n, random -R, version -V
-V, --version-sort
natural sort of (version) numbers within text
Other options:
--batch-size=NMERGE
merge at most NMERGE inputs at once; for more use temp files
-c, --check, --check=diagnose-first
check for sorted input; do not sort
-C, --check=quiet, --check=silent
like -c, but do not report first bad line
--compress-program=PROG
compress temporaries with PROG; decompress them with PROG -d
--debug
annotate the part of the line used to sort, and warn about ques‐
tionable usage to stderr
--files0-from=F
read input from the files specified by NUL-terminated names in
file F; If F is - then read names from standard input
-k, --key=KEYDEF
sort via a key; KEYDEF gives location and type
-m, --merge
merge already sorted files; do not sort
-o, --output=FILE
write result to FILE instead of standard output
-s, --stable
stabilize sort by disabling last-resort comparison
-S, --buffer-size=SIZE
use SIZE for main memory buffer
-t, --field-separator=SEP
use SEP instead of non-blank to blank transition
-T, --temporary-directory=DIR
use DIR for temporaries, not $TMPDIR or /tmp; multiple options
specify multiple directories
--parallel=N
change the number of sorts run concurrently to N
-u, --unique
with -c, check for strict ordering; without -c, output only the
first of an equal run
-z, --zero-terminated
line delimiter is NUL, not newline
--help display this help and exit
--version
output version information and exit
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 be‐
ginning of the preceding whitespace. OPTS is one or more single-letter
ordering options [bdfgiMhnRrV], which override global ordering options
for that key. If no key is given, use the entire line as the key. Use
--debug to diagnose incorrect key usage.
SIZE may be followed by the following multiplicative suffixes: % 1% of
memory, b 1, K 1024 (default), and so on for M, G, T, P, E, Z, Y, R, Q.
*** WARNING *** The locale specified by the environment affects sort
order. Set LC_ALL=C to get the traditional sort order that uses native
byte values.
AUTHOR
Written by Mike Haertel and Paul Eggert.
REPORTING BUGS
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report any translation bugs to <https://translationproject.org/team/>
COPYRIGHT
Copyright © 2023 Free Software Foundation, Inc. License GPLv3+: GNU
GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
SEE ALSO
shuf(1), uniq(1)
Full documentation <https://www.gnu.org/software/coreutils/sort>
or available locally via: info '(coreutils) sort invocation'
GNU coreutils 9.4 April 2024 SORT(1)