linux sort命令的重要用法:按分隔符/字母/数字/月份进行排序

1.指定分隔符,以某一列进行排序并输出

#-t 指定一个分隔符
#-k 后面跟数字,指定按第几列进行排序
#-r 反序排序(升序变成降序)
#按“:”做分隔符,以第3列,也就是用户UID,来从大到小排序
[root@node5 opt]# sort -t ":" -nk3 -r /etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
putong:x:8004:8004::/home/putong:/bin/bash
www:x:8003:8003::/home/www:/bin/bash
stick:x:8002:8002::/home/stick:/bin/bash
esnode:x:8001:8001::/home/esnode:/bin/bash
......

2.输出内容按字母规则进行排序

#默认按字母规则进行排序
[root@node5 opt]# cat /etc/group | sort
adm:x:4:
apache:x:48:
audio:x:63:
bin:x:1:
cdrom:x:11:
daemon:x:2:
......

3.输出内容按数字大小进行排序

[root@node5 opt]# cat num.txt 
21
456
9789
234
24
11
1
31
32
1
3
31

#-n 数字从小到大排序  
[root@node5 opt]# cat num.txt | sort -n

1
1
3
11
21
24
31
31
32
234
456
9789

#数字从大到小排序 
[root@node5 opt]# sort -r num.txt 
9789
456
32
31
31
3
24
234
21
11
1
1

4.输出内容按月份进行排序

[root@node5 opt]# cat month.txt 
December
October
January
November
July
April
February
March
August
May
September
June

#-M 按月份进行排序
[root@node5 opt]# sort -M month.txt 
January
February
March
April
May
June
July
August
September
October
November
December

5.指定目录下的文件按大小进行排序

#把etc目录下所有的文件,按从大到小排序 
[root@node5 ~]# du -h /etc | sort -nr          
posted @ 2020-11-05 01:18  人生的哲理  阅读(3468)  评论(1编辑  收藏  举报