Linux数据排序:sort

这篇博客介绍了Linux中的sort命令,用于对文件数据进行排序。内容包括sort的默认行为,即按照字符串排序,以及如何通过参数如-n进行数字排序。文章详细列举了sort的多个常用参数,如忽略空白、忽略大小写、按数字、浮点数、月份、人类可读数字或版本号排序,以及反向排序、检查输入是否已排序等选项。此外,还提到了与其他命令结合使用的方法,例如按目录大小进行排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 #

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值