👋 欢迎来到吴老二技术博客,专注运维 / 数据库 / 云原生
首页/运维服务/正文

Linux uniq去重方法

吴
吴老二
2020-02-22运维服务2188 次浏览0 条评论
在查看日志中我们会有很多重复的行,为了方便分析需要去重后进行统计或者查看分析,这个时候就需要使用去重,这里说一下去重使用的命令uniq。 uniq的参数:
-c, --count           在每行前加上表示相应行目出现次数的前缀编号
-d, --repeated        只输出重复的行
-D, --all-repeated[=delimit-method    显示所有重复的行
					delimit-method={none(default),prepend,separate}
					以空行为界限
-f, --skip-fields=N   比较时跳过前N 列
-i, --ignore-case     在比较的时候不区分大小写
-s, --skip-chars=N    比较时跳过前N 个字符
-u, --unique          只显示唯一的行
-z, --zero-terminated 使用'\0'作为行结束符,而不是新换行
-w, --check-chars=N   对每行第N 个字符以后的内容不作对照
  --help            显示此帮助信息并退出
  --version         显示版本信息并退出
我们创建一个有重复行的文件,然后针对文件进行去重处理
[root@localhost wulaoer]# cat wulaoer.txt 
2       python3 800     Jan
6       DevOps  300     May
1       Linux   1200    Mar
5       redis   100     Sept
5       redis   100     Sept
4       golong  800     Oct
6       DevOps  300     May
6       DevOps  300     May
3       Ruby    200     Dec
5       redis   100     Sept
4       golong  800     Oct
5       redis   100     Sept
3       Ruby    200     Dec
uniq默认是删除连续的重复行,而不是删除整个文件中有重复的行,这两个概念不一样要分清
[root@localhost wulaoer]# uniq  wulaoer.txt 
2       python3 800     Jan
6       DevOps  300     May
1       Linux   1200    Mar
5       redis   100     Sept
4       golong  800     Oct
6       DevOps  300     May
3       Ruby    200     Dec
5       redis   100     Sept
4       golong  800     Oct
5       redis   100     Sept
3       Ruby    200     Dec
参数'-c'是查找连续出现的行的数量不是找整个文件出现重数
[root@localhost wulaoer]# uniq -c wulaoer.txt 
	  1 2       python3 800     Jan
	  1 6       DevOps  300     May
	  1 1       Linux   1200    Mar
	  2 5       redis   100     Sept
	  1 4       golong  800     Oct
	  2 6       DevOps  300     May
	  1 3       Ruby    200     Dec
	  1 5       redis   100     Sept
	  1 4       golong  800     Oct
	  1 5       redis   100     Sept
	  1 3       Ruby    200     Dec
如果想找文件中所有重复行需要先对文件进行一下排序,排序后的字符串是连续的,就可以进行去重了,下面使用‘-d’参数查看重复的行。
[root@localhost wulaoer]# sort wulaoer.txt  | uniq -d
3       Ruby    200     Dec
4       golong  800     Oct
5       redis   100     Sept
6       DevOps  300     May
使用'-D'参数显示所有重复的行。
[root@localhost wulaoer]# sort wulaoer.txt  | uniq -D
3       Ruby    200     Dec
3       Ruby    200     Dec
4       golong  800     Oct
4       golong  800     Oct
5       redis   100     Sept
5       redis   100     Sept
5       redis   100     Sept
5       redis   100     Sept
6       DevOps  300     May
6       DevOps  300     May
6       DevOps  300     May
使用'-u'显示只出现一次的行
[root@localhost wulaoer]# sort wulaoer.txt | uniq -u
1       Linux   1200    Mar
2       python3 800     Jan
统计出现重复行的行数
[root@localhost wulaoer]# sort wulaoer.txt | uniq -c | sort -rn
	  4 5       redis   100     Sept
	  3 6       DevOps  300     May
	  2 4       golong  800     Oct
	  2 3       Ruby    200     Dec
	  1 2       python3 800     Jan
	  1 1       Linux   1200    Mar
以上是比较常用的参数,其他参数不太常用,而且在以后的工作中不太建议使用uniq,因为uniq使用必须依靠sort排序才方便去重。
文章底部广告位在后台「广告设置」填写广告代码后显示

评论 0

评论需 登录 后发表(注册并验证邮箱)。
还没有评论,来抢沙发~