本文只总结一些常用的用法,更详细的说明见man uniquniq --help

uniq命令

uniq命令主要用于去重。

需要注意的是,不相邻的行不算重复值。

语法格式

1
uniq [OPTION]... [INPUT [OUTPUT]]
OPTION 说明
-c 统计出现的次数
-d 只显示被计算为重复的行
-D 显示所有被计算为重复的行
-c 显示唯一值,即没有重复值的行
-i 忽略大小写
-z 在末尾使用\0,而不是换行符
-s 跳过多少个字符开始比较重复值
-f 跳过多少个字段(field)开始比较重复值
-w 比较重复值时每行比较的最大长度,即对每行多长的字符进行比较

实例

有以下ip地址:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
183.204.149.137
82.190.251.242
183.204.151.206
183.196.249.224
125.119.187.50
125.118.108.202
114.68.82.26
183.204.149.90
183.204.149.90
125.119.185.164
125.119.185.164
183.196.249.224
125.119.185.164
183.156.95.175
183.156.95.175
183.196.249.224
183.156.93.246
183.156.95.175
183.156.95.175
183.156.93.246
183.156.95.175
183.156.95.175
114.68.82.26
183.156.95.175
125.120.25.172
183.156.93.246
183.156.95.175
183.156.95.175
183.156.93.246
114.68.82.26
114.68.82.26
114.68.82.26
114.68.82.26
114.68.82.26
114.68.82.26
114.68.82.26
183.156.95.190
125.119.187.63
114.68.82.26
125.119.187.63
125.119.187.63
125.119.187.63
125.119.185.214
114.68.82.29
114.68.82.26
114.68.82.29
114.68.82.27
114.68.82.26
183.196.253.128
114.68.82.26
114.68.82.27
114.68.82.26
114.68.82.26
114.68.82.26
114.68.82.26
114.68.82.29
183.196.255.237
114.68.82.26
114.68.82.27
114.68.82.26
114.68.82.26
114.68.82.27
183.196.253.128
183.196.253.125
183.196.253.128
183.196.253.128
122.224.179.242
82.186.85.249
82.186.84.18
82.186.85.223
183.204.148.79
183.196.255.237
183.204.163.137
183.204.150.12
183.204.150.12
183.204.150.12

排序去重,重复数由多到少排序:

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
# uniq常和sort一起使用
[root@Rohn ~]#cat ip.txt | sort |uniq -c|sort -nr
20 114.68.82.26
9 183.156.95.175
4 183.196.253.128
4 183.156.93.246
4 125.119.187.63
4 114.68.82.27
3 183.204.150.12
3 183.196.249.224
3 125.119.185.164
3 114.68.82.29
2 183.204.149.90
2 183.196.255.237
1 82.190.251.242
1 82.186.85.249
1 82.186.85.223
1 82.186.84.18
1 183.204.163.137
1 183.204.151.206
1 183.204.149.137
1 183.204.148.79
1 183.196.253.125
1 183.156.95.190
1 125.120.25.172
1 125.119.187.50
1 125.119.185.214
1 125.118.108.202
1 122.224.179.242