7.Linux每日一个命令@uniq

1.uniq说明

Linux uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用。
uniq 可检查文本文件中重复出现的行列。

2.uniq语法

uniq [-cdu][-f<栏位>][-s<字符位置>][-w<字符位置>][--help][--version][输入文件][输出文件]

3.uniq 参数

复制代码
-c或--count 在每列旁边显示该行重复出现的次数。
-d或--repeated 仅显示重复出现的行列。
-f<栏位>或--skip-fields=<栏位> 忽略比较指定的栏位。
-s<字符位置>或--skip-chars=<字符位置> 忽略比较指定的字符。
-u或--unique 仅显示出一次的行列。
-w<字符位置>或--check-chars=<字符位置> 指定要比较的字符。
--help 显示帮助。
--version 显示版本信息。
[输入文件] 指定已排序好的文本文件。如果不指定此项,则从标准读取数据;
[输出文件] 指定输出的文件。如果不指定此选项,则将内容显示到标准输出设备(显示终端)。
复制代码

4.uniq 实例

4.1 去除重复的行

复制代码
[root@ecs-76840553 sed]# cat uniq.txt 
test 30  
test 30  
test 30  
Hello 95  
Hello 95  
Hello 95  
Hello 95  
Linux 85  
Linux 85 
[root@ecs-76840553 sed]# uniq uniq.txt 
test 30  
Hello 95  
Linux 85  
Linux 85 
[root@ecs-76840553 sed]# 
复制代码

4.2 当重复的行并不相邻时,uniq 命令是不起作用的,即若文件内容为以下时,uniq 命令不起作用:此时可以使用sort解决。

复制代码
[root@ecs-76840553 sed]# cat chongfu.txt 
test 30  
Hello 95  
Linux 85 
test 30  
Hello 95  
Linux 85 
test 30  
Hello 95  
Linux 85
[root@ecs-76840553 sed]# uniq chongfu.txt 
test 30  
Hello 95  
Linux 85 
test 30  
Hello 95  
Linux 85 
test 30  
Hello 95  
Linux 85
[root@ecs-76840553 sed]# sort chongfu.txt | uniq
Hello 95  
Linux 85
Linux 85 
test 30  
[root@ecs-76840553 sed]# 
复制代码

4.3 统计各行在文件中出现的次数。

[root@ecs-76840553 sed]# uniq -c uniq.txt 
      3 test 30  
      4 Hello 95  
      1 Linux 85  
      1 Linux 85 
[root@ecs-76840553 sed]# 

4.4 在文件中找出重复的行。

复制代码
[root@ecs-76840553 sed]# cat uniq.txt 
test 30  
test 30  
test 30  
Hello 95  
Hello 95  
Hello 95  
Hello 95  
Li 85  
Li 88 
[root@ecs-76840553 sed]# uniq -d uniq.txt 
test 30  
Hello 95  
[root@ecs-76840553 sed]#
复制代码

4.5 仅显示出现一次的行列

复制代码
[root@ecs-76840553 sed]# cat uniq.txt 
test 30  
test 30  
test 30  
Hello 95  
Hello 95  
Hello 95  
Hello 95  
Li 85  
Li 88 
[root@ecs-76840553 sed]# uniq -d uniq.txt 
test 30  
Hello 95  
[root@ecs-76840553 sed]# 
复制代码

 

posted @   家乐福的搬砖日常  阅读(55)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示