linux如何统计文本文件不重复的行数,linux(centos8):用uniq去除文本中重复的行(去重)...

一,uniq命令的用途

1, 作用:

从输入文件或标准输入中找到相邻的匹配行,

并写入到输出文件或标准输出

2, 使用时通常会搭配sort使用

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,查看uniq命令所属的rpm包

[root@blog nginxlogs]$ whereis uniq

uniq: /usr/bin/uniq /usr/share/man/man1/uniq.1.gz /usr/share/man/man1p/uniq.1p.gz

[root@blog nginxlogs]$ rpm-qf /usr/bin/uniqcoreutils-8.30-6.el8.x86_64

默认已安装到了centos8系统,如果找不到命令或误删除,

可以用dnf来安装

[root@blog nginxlogs]$ dnf install coreutils

三,查看uniq的版本和帮助

1,查看版本

[root@blog nginxlogs]$ uniq --versionuniq (GNU coreutils) 8.30Copyright (C)2018Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later .

This is free software: you are freeto change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.

Written by Richard M. Stallman and David MacKenzie.

2,查看帮助

[root@blog nginxlogs]$ uniq --help

3,查看手册

[root@blog nginxlogs]$ man uniq

四,uniq命令的使用例子

1,增加统计重复出现的次数

#-c: 增加显示当前行重复出现的次数

[root@blog nginxlogs]$ cut -d ' ' -f 1 file_meet.access_log | sort | uniq -c1 106.15.200.123

875 223.72.53.168

9 47.101.200.88

9 47.101.58.46

...

当然可以加一个倒排

[root@blog nginxlogs]$ cut -d ' ' -f 1 file_meet.access_log | sort | uniq -c | sort -k1 -nr875 223.72.53.168

9 47.101.58.46

9 47.101.200.88

1 106.15.200.123

...

2,只显示有重复的行

#-d: 只显示重复出现的行

[root@blog nginxlogs]$ cut -d ' ' -f 1 file_meet.access_log | sort | uniq -d223.72.53.168

47.101.200.88

47.101.58.46

...

3,仅显示没有重复仅出现一次的行

#-u:uniqe

[root@blog nginxlogs]$ cut -d ' ' -f 1 file_meet.access_log | sort | uniq -u106.15.200.123

4,其他参数:

-s: 指定在每行开始处需要忽略的字符数

-i:  忽略每行字母的大小写

五,uniq命令使用需要注意的地方:

如果重复的行不相邻,uniq 命令不会起作用

所以通常我们会搭配sort命令使用,先排序,使重复的行相邻出现,

这样uniq命令就可以生效了

六,查看centos的版本

[root@blog nginxlogs]$ cat /etc/redhat-release

CentOS Linux release8.0.1905 (Core)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值