Linux命令总结--uniq命令
发布时间:2020-12-14 00:26:42 所属栏目:Linux 来源:网络整理
导读:Linux uniq 命令 Linux 命令大全 Linux uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用。 uniq 可检查文本文件中重复出现的行列。 语法 uniq [- cdu ][- f 栏位][- s 字符位置][- w 字符位置][-- help ][-- version ][输入文件
Linux uniq 命令Linux uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用。 uniq 可检查文本文件中重复出现的行列。 语法uniq [-cdu][-f<栏位>][-s<字符位置>][-w<字符位置>][--help][--version][输入文件][输出文件]
参数:
实例文件testfile中第 2、3、5、6、7、9行为相同的行,使用 uniq 命令删除重复的行,可使用以下命令: uniq testfile
testfile中的原有内容为: $ cat testfile #原有内容 test 30 test 30 test 30 Hello 95 Hello 95 Hello 95 Hello 95 Linux 85 Linux 85
使用uniq 命令删除重复的行后,有如下输出结果: $ uniq testfile #删除重复行后的内容 test 30 Hello 95 Linux 85
检查文件并删除文件中重复出现的行,并在行首显示该行重复出现的次数。使用如下命令: uniq -c testfile
结果输出如下: $ uniq -c testfile #删除重复行后的内容 3 test 30 #前面的数字的意义为该行共出现了3次 4 Hello 95 #前面的数字的意义为该行共出现了4次 2 Linux 85 #前面的数字的意义为该行共出现了2次
当重复的行并不相邻时,uniq 命令是不起作用的,即若文件内容为以下时,uniq 命令不起作用: $ cat testfile1 # 原有内容 test 30 Hello 95 Linux 85 test 30 Hello 95 Linux 85 test 30 Hello 95 Linux 85
这时我们就可以使用 sort: $ sort testfile1 | uniq Hello 95 Linux 85 test 30
统计各行在文件中出现的次数: $ sort testfile1 | uniq -c 3 Hello 95 3 Linux 85 3 test 30
在文件中找出重复的行: $ sort testfile1 | uniq -d Hello 95 Linux 85 test 30
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |