⑴Linux系统操作中,文本的内容难免会出现重复行,如果手动删除的话,量多的时候又比较麻烦,那么有什么方法能够快速删除重复行呢?下面小编就给大家介绍下Linux中如何使用uniq命令删除重复行。
⑵一,uniq干什么用的
⑶文本中的重复行,基本上不是我们所要的,所以就要去除掉。linux下有其他命令可以去除重复行,但是我觉得uniq还是比较方便的一个。使用uniq的时候要注意以下二点
⑷,对文本操作时,它一般会和sort命令进行组合使用,因为uniq 不会检查重复的行,除非它们是相邻的行。如果您想先对输入排序,使用sort -u。
⑸,对文本操作时,若域中为先空字符(通常包括空格以及制表符,然后非空字符,域中字符前的空字符将被跳过
⑹二,uniq参数说明
⑺[zhangyBlackGhost ~]$ uniq --help
⑻用法:uniq [选项]。。。 [文件]
⑼从输入文件或者标准输入中筛选相邻的匹配行并写入到输出文件或标准输出。《/p》 《p》不附加任何选项时匹配行将在首次出现处被合并。《/p》 《p》长选项必须使用的参数对于短选项时也是必需使用的。
⑽-c, --count //在每行前加上表示相应行目出现次数的前缀编号
⑾-d, --repeated //只输出重复的行
⑿-D, --all-repeated //只输出重复的行,不过有几行输出几行
⒀-f, --skip-fields=N //-f 忽略的段数,-f 忽略第一段
⒁-i, --ignore-case //不区分大小写
⒂-s, --skip-chars=N //根-f有点像,不过-s是忽略,后面多少个字符 -s 就忽略后面个字符
⒃-u, --unique //去除重复的后,全部显示出来,根mysql的distinct功能上有点像
⒄-z, --zero-terminated end lines with byte, not newline
⒅-w, --check-chars=N //对每行第N 个字符以后的内容不作对照
⒆--help //显示此帮助信息并退出
⒇--version //显示版本信息并退出
⒈其中-z不知道有什么用
⒉三,测试文本文件uniqtest
⒊this is a test
⒋this is a test
⒌this is a test
⒍i am tank
⒎i love tank
⒏i love tank
⒐this is a test
⒑whom have a try
⒒WhoM have a try
⒓you have a try
⒔i want to abroad
⒕those are good men
⒖we are good men
⒗[zhangyBlackGhost mytest]$ uniq -c uniqtest
⒘ this is a test
⒙ i am tank
⒚ i love tank
⒛ this is a test //和第一行是重复的
① whom have a try
② WhoM have a try
③ you have a try
④ i want to abroad
⑤ those are good men
⑥ we are good men
⑦从上例子中我们可以看出,uniq的一个特性,检查重复行的时候,只会检查相邻的行。重复数据,肯定有很多不是相邻在一起的。
⑧[zhangyBlackGhost mytest]$ sort uniqtest |uniq -c
⑨ WhoM have a try
⑩ i am tank
Ⅰ i love tank
Ⅱ i want to abroad
Ⅲ this is a test
Ⅳ those are good men
Ⅴ we are good men
Ⅵ whom have a try
Ⅶ you have a try
Ⅷ这样就可以解决上个例子中提到的问题
Ⅸ[zhangyBlackGhost mytest]$ uniq -d -c uniqtest
Ⅹ this is a test
㈠ i love tank
㈡uniq -d 只显示重复的行
㈢[zhangyBlackGhost mytest]$ uniq -D uniqtest
㈣this is a test
㈤this is a test
㈥this is a test
㈦i love tank
㈧i love tank
㈨uniq -D 只显示重复的行,并且把重复几行都显示出来。他不能和-c一起使用
㈩[zhangyBlackGhost mytest]$ uniq -f -c uniqtest
this is a test
i am tank
i love tank
this is a test
whom have a try
you have a try
i want to abroad
those are good men //只有一行,显示二行
在这里those只有一行,显示的却是重复了,这是因为,-f 忽略了第一列,检查重复从第二字段开始的。
[zhangyBlackGhost mytest]$ uniq -i -c uniqtest
this is a test
i am tank
i love tank
this is a test
whom have a try //一个大写,一个小写
you have a try
i want to abroad
those are good men
we are good men
检查的时候,不区分大小写
[zhangyBlackGhost mytest]$ uniq -s -c uniqtest
this is a test
i am tank
i love tank
this is a test
whom have a try //根上一个例子有什么不同
i want to abroad
those are good men
we are good men
检查的时候,不考虑前个字符,这样whom have a try 就和 you have a try 就一样了。
[zhangyBlackGhost mytest]$ uniq -u uniqtest
i am tank
this is a test
whom have a try
WhoM have a try
you have a try
i want to abroad
those are good men
we are good men
去重复的项,然后全部显示出来
[zhangyBlackGhost mytest]$ uniq -w -c uniqtest
this is a test
i am tank
this is a test
whom have a try
WhoM have a try
you have a try
i want to abroad
those are good men
we are good men
对每行第个字符以后的内容不作检查,所以i am tank 根 i love tank就一样了。
上面就是Linux下使用uniq命令删除重复行命令的方法介绍了,有时文本中的重复行不仅没有用处,还占用空间,快使用uniq命令进行清除吧。