Sort
sort是对文件数据显示进行排序,但是按照字符串形式排序的,而非数字类型排序。
如下:排序时默认将所有数据当做字符串来排序,而非数字大小排序。
如果想要按数字类型排序,需要加参数 -n。
shaphicprb13137:/home # sort test
1
190
2
20
205
2056
21
210
3
58
adf
cd
cva
ghr
s
wed
shaphicprb13137:/home # sort -n test
adf
cd
cva
ghr
s
wed
1
2
3
20
21
58
190
205
210
2056
常用参数:
-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 按通用数值排序(与-n不同,把值当做浮点数排序,且支持科学计数法表示的数值)
-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 questionable 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 指定排序位置,从KEYDEF行排序,如果指定KEYDEF2的话,则是从KEYDEF到KEYDEF2排序
-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 用null作为尾符,而不是换行符
–help display this help and exit
–version output version information and exit
结合其他命令使用,例如:按目录大小从大到小排序
shaphicprb13137:/home # du -sh *
92K dbuser
424K hltreport
396K hltreport.zip
176K ossadm
92K ossuser
4.0K test
shaphicprb13137:/home # du -sh * | sort -nr
424K hltreport
396K hltreport.zip
176K ossadm
92K ossuser
92K dbuser
4.0K test
You have new mail in /var/mail/root
shaphicprb13137:/home #