Chinaunix首页 | 论坛 | 博客
  • 博客访问: 347183
  • 博文数量: 81
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 847
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-25 22:29
个人简介

执一不失,能君万物http://weidian.com/s/284520723?wfr=c

文章分类

全部博文(81)

文章存档

2016年(11)

2015年(70)

我的朋友

分类: LINUX

2015-07-08 16:20:27


点击(此处)折叠或打开

  1. Linux命令uniq的作用是过滤重复部分显示文件内容,这个命令读取输入文件,并比较相邻的行。在正常情况下,第二个及以后更多个重复行将被删去,行比较是根据所用字符集的排序序列进行的。该命令加工后的结果写到输出文件中。输入文件和输出文件必须不同。如果输入文件用“- ”表示,则从标准输入读取。
  2. 使用选项
  3. uniq [选项] 文件
  4. 该命令各选项含义如下:、
  5. – c 显示输出中,在每行行首加上本行在文件中出现的次数。它可取代- u和- d选项。
  6. – d 只显示重复行。
  7. – u 只显示文件中不重复的各行。
  8. – n 前n个字段与每个字段前的空白一起被忽略。一个字段是一个非空格、非制表符的字符串,彼此由制表符和空格隔开(字段从0开始编号)。
  9. +n 前n个字符被忽略,之前的字符被跳过(字符从0开始编号)。
  10. – f n 与- n相同,这里n是字段数。
  11. – s n 与+n相同,这里n是字符数。
  12. [root@localhost ~]# cat fruit.txt
  13. apple:10:2.2
  14. orange:4:4.4
  15. orange:4:4.4
  16. banana:11:5.3
  17. pear:11:4.4
  18. pear:11:4.4
  19. [root@localhost ~]# uniq fruit.txt
  20. apple:10:2.2
  21. orange:4:4.4
  22. banana:11:5.3
  23. pear:11:4.4
  24. #uniq命令不加任何参数,仅显示连续重复的行一次
  25. [root@localhost ~]# uniq -c fruit.txt
  26. 1 apple:10:2.2
  27. 2 orange:4:4.4
  28. 1 banana:11:5.3
  29. 2 pear:11:4.4
  30. #-c 参数显示文件中每行连续出现的次数。
  31. [root@localhost ~]# cat fruit.txt |sort|uniq -c
  32. 1 apple:10:2.2
  33. 1 banana:11:5.3
  34. 2 orange:4:4.4
  35. 2 pear:11:4.4
  36. [root@localhost ~]#
  37. #排序后再显示
  38. [root@localhost ~]# uniq -d fruit.txt
  39. orange:4:4.4
  40. pear:11:4.4
  41. [root@localhost ~]#
  42. -d选项仅显示文件中连续重复出现的行。
  43. [root@localhost ~]# uniq -u fruit.txt
  44. apple:10:2.2
  45. banana:11:5.3
  46. [root@localhost ~]#
  47. #-u选项显示文件中没有连续出现的行。
  48. [root@localhost ~]# cat bb
  49. ai b c d
  50. a i c d
  51. a c d e
  52. b c d e
  53. aa ac d e
  54. [root@localhost ~]# uniq -f 1 bb
  55. ai b c d
  56. a i c d
  57. a c d e
  58. aa ac d e
  59. [root@localhost ~]# uniq -f 2 bb
  60. ai b c d
  61. a c d e
  62. [root@localhost ~]#
  63. #-f 忽略的段数,-f 2 忽略第2段
  64. [root@localhost ~]# cat cc
  65. aa bc dd ee
  66. bb cc dd ee
  67. cc bb dd ee
  68. dd bc ed ee
  69. [root@localhost ~]#
  70. [root@localhost ~]# uniq -f 1 -s 2 cc
  71. aa bc dd ee
  72. cc bb dd ee
  73. dd bc ed ee
  74. [root@localhost ~]# uniq -f 1 -s 3 cc
  75. aa bc dd ee
  76. dd bc ed ee
  77. [root@localhost ~]# uniq -f 1 -s 4 cc
  78. aa bc dd ee
  79. dd bc ed ee
  80. [root@localhost ~]# uniq -f 1 -s 5 cc
  81. aa bc dd ee
  82. [root@localhost ~]#
  83. #-s是忽略n个字符,然后再匹配 【别忘了空格也是一个字符】

阅读(1739) | 评论(0) | 转发(0) |
0

上一篇:合并与排序1-sort

下一篇:合并与排序3-join

给主人留下些什么吧!~~