返回总目录页

sort命令详解

 

 基础命令学习目录首页

 

原文链接:https://blog.csdn.net/shuanghujushi/article/details/51344215

在linux的只用过程中,总是避免不了排序问题。比如,topN问题。linux提供了sort排序命令,支持常用的排序功能。

常用参数

sort命令支持很多参数,常用参数如下:

短参数长参数说明
-n – number-sort 按字符串数值排序,与-g区别为不转为浮点数
-g –general-number-sort 按通用数值排序,支持科学计数法
-f –ignore-case 忽略大小写,默认大小写字母不同
-k –key=POS1[,POS2] 排序从POS1开始,若指定POS2,则POS2结束,否则以pos1排序
-t –field-separator=SEP 指定列的分割符
-r –reverse 降序排序,默认为升序
-h –human-numeric-sort 使用易读性数字(例如: 2K 1G)
-u –unique 去除重复的行
-o –output=FILE 将输出写入文件

常用用法举例

1.默认排序

默认情况下,sort命令,以字母序进行文本排序。如下:

shuanghu@shuanghu:tmp$cat word.txt
one
two 
three
four

shuanghu@shuanghu:tmp$sort word.txt
four
one
three
two

2.数字排序

如果想对数字进行排序,可以使用-n参数

shuanghu@shuanghu:tmp$ cat num.txt 
100
20
3
shuanghu@shuanghu:tmp$ sort num.txt -n
3
20
100

3.指定列排序

sort排序的时候,可以按字段分割的数据进行排序。-t参数表示行的分割字符,-k表示第几列。当然,可以进行降序排序,-r参数可以实现。
下面是对passwd文件,以冒号(:)进行分割,然后对第三列以数字方式进行降序排序。

shuanghu@shuanghu:etc$ cat passwd 
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
shuanghu@shuanghu:etc$ sort -t ':' -k 3 -nr passwd 
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
games:x:5:60:games:/usr/games:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
sys:x:3:3:sys:/dev:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin

4.文件夹大小排序

在du的时候,加上-h可以使用易读性数字,比如2k,1g,3M这种。sort也支持-h参数。
比如,du一个文件夹下的目录大小后,想以文件大小进行排序。由于du -h的结果是3k,2M,1G这种,不能简单的按数字排序。所以,可以使用-h参数。具体如下:

shuanghu@shuanghu:tmp$ du -h
2.0G    ./test2
4.0K    ./test3
316M    ./test
2.3G    .
shuanghu@shuanghu:tmp$ du -h |sort -hr
2.3G    .
2.0G    ./test2
316M    ./test
4.0K    ./test3

5.系统进程内存占用排序

查看系统进程中,内存占用最多的前5个进程信息

shuanghu@shuanghu:tmp$ ps aux|sort -gr -k 4|head -n 5
shuanghu  1740 15.7  4.6 1506764 189872 ?      Sl    5月07 142:08 compiz
root      1304  2.1  1.9 338928 80208 tty7     Ssl+  5月07  19:29 /usr/bin/X -core :0 -seat seat0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch
shuanghu  1933  0.0  1.1 1074520 46708 ?       Sl    5月07   0:00 /usr/lib/evolution/evolution-calendar-factory
shuanghu  1833  0.0  0.8 974900 34468 ?        Sl    5月07   0:01 nautilus -n
shuanghu  2111  0.0  0.6 655712 24920 ?        Sl    5月07   0:16 gnome-terminal

 

6.对文件内容进行去重

如果文件内容有很多重复的,需要进行去重。sort也是支持的,可以通过-u参数使用

shuanghu@shuanghu:tmp$cat word.txt
one
two
two
three
three
three
four
four
four

shuanghu@shuanghu:tmp$sort -u word.txt
four
one
three
two

7.将sort输出内容写入文件

在shell中,一般将控制台内容写入文件,可以使用重定向,但如果想把sort的排序内容写回文件,则不能使用重定向。则需要-o参数。具体如下:

shuanghu@shuanghu:tmp$cat word.txt
one
two 
three
four
shuanghu@shuanghu:tmp$sort word.txt > word.txt
shuanghu@shuanghu:tmp$cat word.txt #输出为空

shuanghu@shuanghu:tmp$sort word.txt -o word.txt
shuanghu@shuanghu:tmp$sort -u word.txt
four
one
three
two

 

posted @ 2018-09-01 17:12  马昌伟  阅读(12485)  评论(1编辑  收藏  举报
博主链接地址:https://www.cnblogs.com/machangwei-8/