您当前所在位置:首页linuxLinux uniq命令怎么使用

Linux uniq命令怎么使用

更新:2024-05-16 13:37:26编辑:dashuai归类:linux人气:45

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

uniq 可检查文本文件中重复出现的行列。

语法:

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

参数:

-c或--count 在每列旁边显示该行重复出现的次数。

-d或--repeated 仅显示重复出现的行列。

-f<栏位>或--skip-fields=<栏位> 忽略比较指定的栏位。

-s<字符位置>或--skip-chars=<字符位置> 忽略比较指定的字符。

-u或--unique 仅显示出一次的行列。

-w<字符位置>或--check-chars=<字符位置> 指定要比较的字符。

--help 显示帮助。

--version 显示版本信息。

[输入文件] 指定已排序好的文本文件。如果不指定此项,则从标准读取数据;

[输出文件] 指定输出的文件。如果不指定此选项,则将内容显示到标准输出设备(显示终端)。

实例:

文件testfile中第 2、3、5、6、7、9行为相同的行,使用 uniq 命令删除重复的行,可使用以下命令:

1
uniq

testfile中的原有内容为:

1
2
3
4
5
6
7
8
9
10
$ cat testfile      #原有内容  
test 30  
test 30  
test 30  
Hello 95  
Hello 95  
Hello 95  
Hello 95  
Linux 85  
Linux 85

使用uniq 命令删除重复的行后,有如下输出结果:

1
2
3
4
$ uniq testfile     #删除重复行后的内容  
test 30  
Hello 95  
Linux 85

检查文件并删除文件中重复出现的行,并在行首显示该行重复出现的次数。使用如下命令:

1
uniq

结果输出如下:

1
2
3
4
$ uniq -c testfile      #删除重复行后的内容  
3 test 30             #前面的数字的意义为该行共出现了3次  
4 Hello 95            #前面的数字的意义为该行共出现了4次  
2 Linux 85

当重复的行并不相邻时,uniq 命令是不起作用的,即若文件内容为以下时,uniq 命令不起作用:

1
2
3
4
5
6
7
8
9
10
$ cat testfile1      # 原有内容 
test 30  
Hello 95  
Linux 85 
test 30  
Hello 95  
Linux 85 
test 30  
Hello 95  
Linux 85

这时我们就可以使用 sort:

1
2
3
4
$ sort  testfile1 | uniq
Hello 95  
Linux 85 
test 30

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

1
2
3
4
$ sort testfile1 | uniq -c
   3 Hello 95  
   3 Linux 85 
   3 test 30

在文件中找出重复的行:

1
2
3
4
$ sort testfile1 | uniq -d
Hello 95  
Linux 85 
test 30


我告诉你msdn版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

linux
linux硬链接不能链接目录文件如何解决 115浏览器

游客 回复需填写必要信息