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这个命令要排序文件或者文件内容时,是比较常用到的,常搭配管道符一起使用,可以显示排序的数值。若觉得内容还行的,可以一键三连支持一下!

  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Linux sort命令用于将文本文件内容进行排序sort命令将每一行作为一个单位进行比较,按照ASCII码值进行排序,并将排序结果输出。默认情况下,sort命令按照字母顺序进行排序。可以使用sort命令与其他命令结合使用,如ls、cat等,将查询结果按照我们的要求进行排序。\[1\] 例如,如果我们有两个文件baichao.txt和baichao1.txt,我们可以使用sort命令将它们合并并按照默认的字母顺序排序输出。\[2\] 如果我们要对包含数字的文件进行排序,可以使用sort命令的-n选项。这样可以按照数字的大小进行排序,而不是按照字符的顺序进行排序。\[3\] 总之,Linux sort命令是一个非常有用的工具,可以帮助我们对文本文件进行排序操作。 #### 引用[.reference_title] - *1* [Linux命令sort命令](https://blog.csdn.net/carefree2005/article/details/115553478)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [shell:sort(将文本文件内容加以排序)](https://blog.csdn.net/weixin_40179091/article/details/113698706)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linux学习中

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值