Linux基础命令-sort内容排序
Linux基础命令-chattr更改文件隐藏属性_Linux学习中的博客-CSDN博客
Linux基础命令-chmod修改文件权限_Linux学习中的博客-CSDN博客
文章目录
前言
一 命令的介绍
二 语法及参数
2.1 使用help查看命令语法
2.2 常用参数
三 参考实例
3.1 按字母顺序排序
3.2 按数字大小进行排序
3.3 以冒号为间隔符,选定第三列进行数字大小的排序
3.4 两个文件一起排序,并将结果写入到一个文件
3.5 以降序的方式排序
3.6 检查文件是否已经排序
3.7 排序内容,若有重复只显示一行
3.8 搭配管道符一起使用
3.9 按照前三位月份字母进行排序
总结
前言
在linux中,总有需要排序数值从大到小的瞬间,有时候要看什么文件比较大,把它先列在前面,就可以用这个命令;当然sort还能办到去重的功能,一起来看看这个命令的使用吧。
一 命令的介绍
sort命令的功能是对文件内容进行排序,将文件的每一行作为一个单位,相互比较,比较的原则是从首字符向后依次按ASCII码值进行比较,最后将它们按升序输出。
二 语法及参数
2.1 使用help查看命令语法
[root@localhost ~]# sort --help 用法:sort [选项]... [文件]... 或:sort [选项]... --files0-from=F
语法: sort 【选项】文件
2.2 常用参数
| -b | 忽略每行前面开始出的空格字符 |
| -c | 检查文件是否已经按照顺序排序 |
| -d | 除字母、数字及空格字符外,忽略其他字符 |
| -f | 将小写字母视为大写字母 |
| -i | 除040至176之间的ASCII字符外,忽略其他字符 |
| -m | 合并文件,并不排序 |
| -M | 将前面3个字母依照月份的缩写进行排序 |
| -n | 依照数值的大小排序 |
| -o | 将排序后的结果存入指定的文件 |
| -r | 以相反的顺序来排序 |
| -t | 指定排序时所用的栏位分隔字符 |
| -u | 去重,多行一样的显示一行 |
| -k | 指定需要排序的栏位 |
三 参考实例
3.1 按字母顺序排序
[root@localhost ~]# cat fruit.txt banana pear apple orange [root@localhost ~]# sort fruit.txt apple banana orange pear
注意:使用命令对文件内容进行排序,并不会写入文件中或者让文件中的顺序进行修改;当然如果想写入到文件中,可以使用-o参数,写入到指定文件里。
[root@localhost ~]# sort fruit.txt -n -o fruit1.txt [root@localhost ~]# cat fruit1.txt apple banana orange pear
3.2 按数字大小进行排序
按文件数字大小进行排序,一定是需要加-n参数的,因为sort默认是从首行内容开始依次排序。
[root@localhost ~]# cat 1.txt 123 12 35 50 563 211 666 8 90 5 2 [root@localhost ~]# sort 1.txt 12 123 2 211 35 5 50 563 666 8 90 [root@localhost ~]# sort -n 1.txt 2 5 8 12 35 50 90 123 211 563 666
3.3 以冒号为间隔符,选定第三列进行数字大小的排序
[root@localhost ~]# sort -t : -k 3 -n passwd | head root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin
3.4 两个文件一起排序,并将结果写入到一个文件
[root@localhost ~]# sort 1.txt 3.txt -bn > 6.txt [root@localhost ~]# cat 6.txt 1 2 2 5 8 12 12 35 50 90 90 123 ...... [root@localhost ~]# sort 1.txt 3.txt -bn -o 8.txt [root@localhost ~]# cat 8.txt 1 2 2 5 8 12 12 35
3.5 以降序的方式排序
默认是以升序的形式排序,降序则代表将内容取反
[root@localhost ~]# sort -nr 1.txt 666 563 211 123 90 50 35 12 8 5 2
3.6 检查文件是否已经排序
若无排序会显示无序,文件内容已排序不会输出内容
[root@localhost ~]# sort -c 1.txt sort:1.txt:2:无序: 12 [root@localhost ~]# echo $? 1 [root@localhost ~]# cat 10.txt 1 2 3 4 5 64 700 [root@localhost ~]# sort -c 10.txt [root@localhost ~]# echo $? 0
3.7 排序内容,若有重复只显示一行
[root@localhost ~]# cat b.txt apple bear orange apple [root@localhost ~]# sort -u b.txt apple bear orange
3.8 搭配管道符一起使用
把文件从大到小进行排序,ll -S也可以进行排序。
[root@localhost ~]# ll -a |sort -nr |head 总用量 132 -rwxr-xr-x. 1 root root 942 2月 18 21:54 rm_file.sh -rwxr-xr-x. 1 root root 0 2月 17 16:22 ping.sh -rw-r--r--. 1 root root 73 2月 19 10:02 9.txt -rw-r--r--. 1 root root 73 2月 19 09:58 8.txt -rw-r--r--. 1 root root 73 2月 19 09:56 6.txt -rw-r--r--. 1 root root 73 2月 19 09:53 5.txt -rw-r--r--. 1 root root 70 2月 19 09:48 2.txt -rw-r--r--. 1 root root 49 2月 19 10:14 b.txt -rw-r--r--. 1 root root 45 2月 19 10:03 1.txt [root@localhost ~]# ll -S |head 总用量 84 -rw-r--r--. 1 root root 2356 2月 19 09:44 passwd -rwxr-xr-x. 1 root root 942 2月 18 21:54 rm_file.sh -rw-r--r--. 1 root root 146 2月 19 09:55 n -rw-r--r--. 1 root root 146 2月 19 09:55 nc -rw-r--r--. 1 root root 73 2月 19 09:53 5.txt -rw-r--r--. 1 root root 73 2月 19 09:56 6.txt -rw-r--r--. 1 root root 73 2月 19 09:58 8.txt
3.9 按照前三位月份字母进行排序
[root@localhost ~]# sort -M b.txt Aprd Febn Jana Junef Marc Maye
总结
sort这个命令要排序文件或者文件内容时,是比较常用到的,常搭配管道符一起使用,可以显示排序的数值。若觉得内容还行的,可以一键三连支持一下!

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/373f66fb29.html
