Linux sort命令:对文本文件的行排序
sort 是 Linux 的排序命令,而且可以依据不同的数据类型来进行排序。sort 将文件的每一行作为一个单位,相互比较。比较原则是从首字符向后,依次按 ASCII 码值进行比较,最后将它们按升序输出。
Sort是用于对单个或多个文本文件内容进行排序的Linux程序。Sort命令以空格作为字段分隔符,将一行分割为多个关键字对文件进行排序。需要注意的是除非你将输出重定向到文件中,否则Sort命令并不对文件内容进行实际的排序(即文件内容没有修改),只是将文件内容按有序输出。
sort选项:
-f:忽略大小写;
-b:忽略每行前面的空白部分;
-n:以数值型进行排序,默认使用字符串排序;
-r:反向排序;
-u:删除重复行。就是 uniq 命令;
-t:指定分隔符,默认分隔符是制表符;
-k [n,m]:按照指定的字段范围排序。从第 n 个字段开始,到第 m 个字(默认到行尾);
sort 命令默认是用每行开头的第一个字符来进行排序的,比如:
sort —help
1 | [root@localhost Linux_Test]# sort --help |
man sort
1 | SORT(1) FSF SORT(1) |
sort排序示例
sort 文件:默认排序( 整行进行ASCII字符升序)
1 | [root@localhost Linux_Test]# cat sortFile.txt |
sort -c:检查是否已经排好序
1 | [root@localhost Linux_Test]# cat sortFile.txt |
sort -n:数字按照算术值大小排序
-n 选项 Numberic 对于 数字按照算术值大小排序,而不是按照字符串比较
规则,例如 123 与 67
按算术值排序的话123比67大,如果按照字符串排序的话67比123大。
按字符串排序
1 | [root@localhost Linux_Test]# vim sortFile.txt |
sort -g:浮点数排序
1 | [root@localhost Linux_Test]# sort -n sortFile.txt |
sort -n和sort -g的区别
1 | -g, --general-numeric-sort |
- 通用数字排序(-g)将数字比较为浮点数,这可以采用科学记数法,例如1.234E10,但速度较慢并且容易出现舍入错误(1.2345678可能在1.2345679之后).
- 数字排序(-n)只是一种常规的字母排序,知道10在9之后。
按数值排序
1 | [root@localhost Linux_Test]# sort -n sortFile.txt |
sort -u:去除排序结果中的重复行
它的作用很简单,就是在输出行中去除重复行。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28[root@localhost Linux_Test]# cat sortFile.txt
211
121
54
54
65
65
65
87
98
[root@localhost Linux_Test]# sort -n sortFile.txt
54
54
65
65
65
87
98
121
211
[root@localhost Linux_Test]# sort -n -u sortFile.txt
54
65
87
98
121
211
[root@localhost Linux_Test]#
sort -r:降序排序
sort默认的排序方式是升序,如果想改成降序,就加个-r就搞定了。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31[root@localhost Linux_Test]# cat sortFile.txt
211
121
54
54
65
65
65
87
98
[root@localhost Linux_Test]# sort -n sortFile.txt
54
54
65
65
65
87
98
121
211
[root@localhost Linux_Test]# sort -n -r sortFile.txt
211
121
98
87
65
65
65
54
54
[root@localhost Linux_Test]#
sort -o 文件:将结果写入到文件而非标准输出
由于sort默认是把结果输出到标准输出,所以需要用重定向才能将结果写入文件,形如1
sort filename > newfile
但是,如果你想把排序结果输出到原文件中,用重定向可就不行了。1
2
3
4
5
6
7
8
9
10[root@localhost Linux_Test]# cat sortFile.txt
98
87
65
54
32
21
[root@localhost Linux_Test]# sort sortFile.txt >sortFile.txt
[root@localhost Linux_Test]# cat sortFile.txt
[root@localhost Linux_Test]#
可以看到,将文件排序后再重定向到该文件后,该文件的内容会被清空。
就在这个时候,-o选项出现了,它成功的解决了这个问题,让你放心的将结果写入原文件。这或许也是-o比重定向的唯一优势所在。
将原文件的内容排序后覆盖原文件
1 | [root@localhost Linux_Test]# vim sortFile.txt |
如果想要指定排序的字段,则需要使用”-t”选项指定分隔符,并使用”-k”选项指定字段号。
sort -t:指定分隔符
默认分隔符是制表符;
对于特殊符号(如制表符),可使用类似于-t$’\t’或-t’ctrl+v,tab’(先按ctrl+v,然后按tab键)的方法实现。
sort -k [n,m]:按照指定的字段范围排序。
从第 n 个字段开始,到第 m 个字(默认到行尾);
测试文件:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16[root@localhost Linux_Test]# cat ls_l_out.txt
总用量 120
-rw-r--r--. 1 root root 36 4月 24 13:44 cat_test.txt
-rw-r--r--. 1 root root 30 4月 20 18:21 date_test.txt
-rw-r--r--. 1 root root 592 4月 24 13:19 info_a.log
-rw-r--r--. 1 root root 294 4月 23 14:14 less_test.txt
-rw-r--r--. 1 root root 0 4月 26 15:44 ls_l_out.txt
-rw-r--r--. 1 root root 75517 4月 23 14:21 man_less.txt
-rw-r--r--. 1 root root 9 4月 22 16:47 more_test2.txt
-rw-r--r--. 1 root root 23 4月 22 17:04 more_test3.txt
-rw-r--r--. 1 root root 83 4月 22 17:19 more_test4.txt
-rw-r--r--. 1 root root 294 4月 22 16:22 more_test.txt
-rw-r--r--. 1 root root 48 4月 26 15:38 sortFile.txt
-rw-r--r--. 1 root root 27 4月 14 01:48 vi_replaceAllTest.txt
-rw-r--r--. 1 root root 36 4月 24 14:00 wc_test.txt
[root@localhost Linux_Test]#
按第5个字段进行排序:
[root@localhost Linux_Test]# sort -k 5,5 ls_l_out.txt 总用量 120 -rw-r--r--. 1 root root 0 4月 26 15:44 ls_l_out.txt -rw-r--r--. 1 root root 23 4月 22 17:04 more_test3.txt -rw-r--r--. 1 root root 27 4月 14 01:48 vi_replaceAllTest.txt -rw-r--r--. 1 root root 294 4月 22 16:22 more_test.txt -rw-r--r--. 1 root root 294 4月 23 14:14 less_test.txt -rw-r--r--. 1 root root 30 4月 20 18:21 date_test.txt -rw-r--r--. 1 root root 36 4月 24 13:44 cat_test.txt -rw-r--r--. 1 root root 36 4月 24 14:00 wc_test.txt -rw-r--r--. 1 root root 48 4月 26 15:38 sortFile.txt -rw-r--r--. 1 root root 592 4月 24 13:19 info_a.log -rw-r--r--. 1 root root 75517 4月 23 14:21 man_less.txt -rw-r--r--. 1 root root 83 4月 22 17:19 more_test4.txt -rw-r--r--. 1 root root 9 4月 22 16:47 more_test2.txt [root@localhost Linux_Test]#
按第9个字段排序:
[root@localhost Linux_Test]# sort -k 9,9 ls_l_out.txt 总用量 120 -rw-r--r--. 1 root root 36 4月 24 13:44 cat_test.txt -rw-r--r--. 1 root root 30 4月 20 18:21 date_test.txt -rw-r--r--. 1 root root 592 4月 24 13:19 info_a.log -rw-r--r--. 1 root root 294 4月 23 14:14 less_test.txt -rw-r--r--. 1 root root 0 4月 26 15:44 ls_l_out.txt -rw-r--r--. 1 root root 75517 4月 23 14:21 man_less.txt -rw-r--r--. 1 root root 9 4月 22 16:47 more_test2.txt -rw-r--r--. 1 root root 23 4月 22 17:04 more_test3.txt -rw-r--r--. 1 root root 83 4月 22 17:19 more_test4.txt -rw-r--r--. 1 root root 294 4月 22 16:22 more_test.txt -rw-r--r--. 1 root root 48 4月 26 15:38 sortFile.txt -rw-r--r--. 1 root root 27 4月 14 01:48 vi_replaceAllTest.txt -rw-r--r--. 1 root root 36 4月 24 14:00 wc_test.txt [root@localhost Linux_Test]#
当然,”-k”选项可以直接使用”-k 9”,代表从第三个字段到行尾都排序(第一个字段先排序,如果一致,则第二个字段再排序,直到行尾)。
sort -t ‘分隔符’ -k 开始字段,结束字段
测试文件:1
2
3
4
5
6
7[root@localhost Linux_Test]# cat sort_t_k.txt
bnana:30:5.5
bnana:12:9.8
apple:10:2.5
pear:90:2.3
orange:20:3.4
[root@localhost Linux_Test]#
这个文件有三列,列与列之间用冒号隔开了,第一列表示水果类型,第二列表示水果数量,第三列表示水果价格。
那么我想以水果价格来排序,也就是以第3列来排序,如何利用sort实现?
幸好,sort提供了-t选项,后面可以设定间隔符。
指定了间隔符之后,就可以用-k来指定列数了。
[root@localhost Linux_Test]# sort -t ':' -k 3,3 sort_t_k.txt pear:90:2.3 apple:10:2.5 orange:20:3.4 bnana:30:5.5 bnana:12:9.8 [root@localhost Linux_Test]#
按第2列进行排序:
[root@localhost Linux_Test]# sort -t ':' -k 2,2 sort_t_k.txt apple:10:2.5 bnana:12:9.8 orange:20:3.4 bnana:30:5.5 pear:90:2.3 [root@localhost Linux_Test]#按第1列到第2列进行排序
[root@localhost Linux_Test]# sort -t ':' -k 1,2 sort_t_k.txt apple:10:2.5 bnana:12:9.8 bnana:30:5.5 orange:20:3.4 pear:90:2.3 [root@localhost Linux_Test]#
参考资料
http://c.biancheng.net/view/996.html
https://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html
https://man.linuxde.net/sort
http://fancyerii.github.io/2019/06/15/sort/
https://www.linuxidc.com/Linux/2017-08/146605.htm