sort [OPTION]... [FILE]...
!subtitle:功能
将文本文件的行进行排序。
!subtitle:类型
可执行文件(/usr/bin/sort),属于 coreutils。
!subtitle:参数
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'),非数值视为 0
random - 同 -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
!subtitle:默认排序(字典序)
$ sort 1.txt
10
1e2
2
3.5
3M
50K
apple
Apple
!banana
Feb
Jan
NaN
v1
v10
v2
!subtitle:按数值排序
$ sort -n 1.txt
apple
Apple
!banana
Feb
Jan
NaN
v1
v10
v2
1e2
2
3M
3.5
10
50K
!subtitle:按广义数值排序
$ sort -g 1.txt
apple
Apple
!banana
Feb
Jan
v1
v10
v2
NaN
2
3M
3.5
10
50K
1e2
!subtitle:含单位的数值排序
$ sort -h 1.txt
apple
Apple
!banana
Feb
Jan
NaN
v1
v10
v2
1e2
2
3.5
10
50K
3M
!subtitle:版本号排序
$ sort -V 1.txt
1e2
2
3M
3.5
10
50K
Apple
Feb
Jan
NaN
apple
v1
v2
v10
!banana
!subtitle:月份排序
$ sort -M 1.txt
10
1e2
2
3.5
3M
50K
apple
Apple
!banana
NaN
v1
v10
v2
Jan
Feb
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)