一,uniq命令的用途
1, 作用:
從輸入文件或標准輸入中找到相鄰的匹配行,
並寫入到輸出文件或標准輸出
2, 使用時通常會搭配sort使用
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 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/uniq coreutils-8.30-6.el8.x86_64
默認已安裝到了centos8系統,如果找不到命令或誤刪除,
可以用dnf來安裝
[root@blog nginxlogs]$ dnf install coreutils
三,查看uniq的版本和幫助
1,查看版本
[root@blog nginxlogs]$ uniq --version uniq (GNU coreutils) 8.30 Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. This is free software: you are free to 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 -c 1 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 -nr 875 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 -d 223.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 -u 106.15.200.123
4,其他參數:
-s: 指定在每行開始處需要忽略的字符數
-i: 忽略每行字母的大小寫
五,uniq命令使用需要注意的地方:
如果重復的行不相鄰,uniq 命令不會起作用
所以通常我們會搭配sort命令使用,先排序,使重復的行相鄰出現,
這樣uniq命令就可以生效了
六,查看centos的版本
[root@blog nginxlogs]$ cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core)