Linux uniq

uniq

uniq 在读取行时会对它们进行比较并将只除去两个或更多的连续行


参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-c, --count           prefix lines by the number of occurrences
-d, --repeated only print duplicate lines, one for each group
-D print all duplicate lines
--all-repeated[=METHOD] like -D, but allow separating groups
with an empty line;
METHOD={none(default),prepend,separate}
-f, --skip-fields=N avoid comparing the first N fields
--group[=METHOD] show all items, separating groups with an empty line;
METHOD={separate(default),prepend,append,both}
-i, --ignore-case ignore differences in case when comparing
-s, --skip-chars=N avoid comparing the first N characters
-u, --unique only print unique lines
-z, --zero-terminated line delimiter is NUL, not newline
-w, --check-chars=N compare no more than N characters in lines
--help display this help and exit
--version output version information and exit

最为常用的即为: -c -u -d

示例

1
2
3
4
5
6
7
root@Kali:~# cat uniqfile 
12334
12334
1234567
Hello,world
1234567
1234567
Uniq 使用去除连续重复行
1
2
3
4
5
root@Kali:~# uniq uniqfile 
12334
1234567
Hello,world
1234567
-c 参数 显示连续个数
1
2
3
4
5
root@Kali:~# uniq -c uniqfile 
2 12334
1 1234567
1 Hello,world
2 1234567
-d 参数 显示重复行
1
2
3
root@Kali:~# uniq -d uniqfile 
12334
1234567
-u 参数 显示唯一
1
2
3
root@Kali:~# uniq -u uniqfile 
1234567 ## 实际还是重复,但是不连续,因此需要配合sort
Hello,world
配合 sort 去除所有重复行
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
root@Kali:~# sort uniqfile 
12334
12334
1234567
1234567
1234567
Hello,world

root@Kali:~# sort uniqfile | uniq -c ## 存在个数
2 12334
3 1234567
1 Hello,world


root@Kali:~# sort uniqfile | uniq -u
Hello,world ## 唯一


root@Kali:~# sort uniqfile | uniq -d ##输出重复
12334
1234567


root@Kali:~# sort uniqfile |uniq ## 即可进行重定向输出
12334
1234567
Hello,world
欣赏此文? 求鼓励,求支持!