Linux uniq 命令

Linux uniq 命令

uniq 命令 uniq 命令可以去除排序过的文件中的重复行,因此 uniq 经常和 sort 合用。也就是说,为了使 uniq 起作用,所有的重复行必须是相邻的

uniq 语法

[root@www ~]# uniq [-icu]
选项与参数:
-i   :忽略大小写字符的不同;
-c或--count 在每列旁边显示该行重复出现的次数
-u或--unique 仅显示出一次的行列
-d或--repeated 仅显示重复出现的行列

实战

testfile 的内容如下

hello
world
friend
hello
world
hello

直接去重未经排序的文件,将会发现没有任何行被删除

#uniq testfile  
hello
world
friend
hello
world
hello

排序文件,并进行重.同时在行首位置输出该行重复的次数

➜  ~ sort test.txt | uniq -c
   1 friend
   3 hello
   2 world

仅显示存在重复的行,并在行首显示该行重复的次数

#sort testfile | uniq -dc
3 hello
2 world

仅显示不重复的行

sort testfile | uniq -u
friend  

参考资料:
https://www.runoob.com/linux/linux-comm-uniq.html

你可能感兴趣的:(Linux uniq 命令)