- 语法
sort [options] [file]
-n | 按照数字大小进行排序 |
-r | 倒序排列 |
-t | 指定分隔符 |
-k | 按指定区间排序 -k3和-k3,3的含义不同,如果不指定结尾会默认从3列开始到行尾 如果同一行需要按两列排序的话,必须指定结尾! |
-b | 忽略每行开头存在的空格 |
-u | 去除重复行 |
sort -n -t. -k3,3 -k4.1,4.3 arp.txt
-k3,3 从第三列起始到第三列结束
-k4.1,4.3 从第三列第一个字符到第三列第三个字符
逗号 , 连接的是字段
句号 . 连接的是字符
[root@localhost logs]# cat sort.txt
10.10.10.4
10.10.10.4
10.10.10.4
10.10.10.4
10.10.10.5
10.10.10.5
10.10.10.8
10.10.10.4
[root@localhost logs]# sort sort.txt
10.10.10.4
10.10.10.4
10.10.10.4
10.10.10.4
10.10.10.4
10.10.10.5 不加选项
10.10.10.5
10.10.10.8
[root@localhost logs]# sort -n sort.txt
10.10.10.4
10.10.10.4
10.10.10.4
10.10.10.4
10.10.10.4
10.10.10.5
10.10.10.5 -n选项作用
10.10.10.8
[root@localhost logs]# sort -nr sort.txt
10.10.10.8
10.10.10.5
10.10.10.5
10.10.10.4
10.10.10.4
10.10.10.4 -r选项作用
10.10.10.4
10.10.10.4
[root@localhost logs]# sort -ur sort.txt
10.10.10.8
10.10.10.5 -u选项作用
10.10.10.4
[root@localhost logs]# echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@localhost logs]# cat test.txt
10.10.10.4 r
10.10.10.4 g
10.10.10.4 a
10.10.10.5 n
10.10.10.4 q
10.10.10.8 l
[root@localhost logs]# sort test.txt
10.10.10.4 a
10.10.10.4 g
10.10.10.4 q
10.10.10.4 r
10.10.10.5 n 可以看出是先按数字然后按字母排序
10.10.10.8 l
[root@localhost logs]# sort -t " " -k2 test.txt
10.10.10.4 a
10.10.10.4 g
10.10.10.8 l
10.10.10.5 n
10.10.10.4 q 以空格为分隔符,以第二列进行排序
10.10.10.4 r
[root@localhost logs]# cat arp.txt
192.168.111.1 00:50:56:c0:00:08
192.168.113.2 00:50:56:e7:84:7f
192.168.112.3 00:50:56:e7:84:72
192.168.111.2 00:50:56:e7:84:51
192.168.111.43 00:50:56:e7:84:6f
192.168.111.140 00:50:56:e7:84:3d
192.168.111.5 00:50:56:e7:84:09
192.168.111.6 00:50:56:e7:84:7a
[root@localhost logs]# sort -n -t. -k3,3 -k4.1,4.3 arp.txt
192.168.111.1 00:50:56:c0:00:08
192.168.111.2 00:50:56:e7:84:51
192.168.111.5 00:50:56:e7:84:09
192.168.111.6 00:50:56:e7:84:7a
192.168.111.43 00:50:56:e7:84:6f
192.168.111.140 00:50:56:e7:84:3d
192.168.112.3 00:50:56:e7:84:72 以第三列和第四列进行排序
192.168.113.2 00:50:56:e7:84:7f
[root@localhost logs]# sort -n -t. -k3 -k4.1,4.3 arp.txt
192.168.111.1 00:50:56:c0:00:08
192.168.111.140 00:50:56:e7:84:3d
192.168.111.2 00:50:56:e7:84:51
192.168.111.43 00:50:56:e7:84:6f
192.168.111.5 00:50:56:e7:84:09
192.168.111.6 00:50:56:e7:84:7a
192.168.112.3 00:50:56:e7:84:72 在第三列的时候没有指定结尾,输出内容错误
192.168.113.2 00:50:56:e7:84:7f