使用awk+sort+uniq进行文本分析
2015-11-26 10:01
573 查看
1、uniq命令
uniq - report or omit repeated lines
介绍:uniq对指定的ASCII文件或标准输入进行唯一性检查,以判断文本文件中重复出现的行。常用于系统排查及日志分析
命令格式:
uniq [OPTION]... [File1 [File2]]
uniq从已经排序好的文本文件File1中删除重复的行,输出到标准标准输出或File2。常作为过滤器,配合管道使用。
在使用uniq命令之前,必须确保操作的文本文件已经过sort排序,若不带参数运行uniq,将删除重复的行。
常见参数:
-c, --count prefix lines by the number of occurrences 去重后计数
2、实战演练
测试数据:
处理一下文件内容,将域名取出并根据域名进行计数排序处理(百度和sohu面试题)
分析:此类问题是运维工作中最常见的问题。可以演变成分析日志,查看TCP各个状态连接数,查看单IP连接数排名等等。
本文出自 “万越天的架构师之路” 博客,请务必保留此出处http://wanyuetian.blog.51cto.com/3984643/1716971
uniq - report or omit repeated lines
介绍:uniq对指定的ASCII文件或标准输入进行唯一性检查,以判断文本文件中重复出现的行。常用于系统排查及日志分析
命令格式:
uniq [OPTION]... [File1 [File2]]
uniq从已经排序好的文本文件File1中删除重复的行,输出到标准标准输出或File2。常作为过滤器,配合管道使用。
在使用uniq命令之前,必须确保操作的文本文件已经过sort排序,若不带参数运行uniq,将删除重复的行。
常见参数:
-c, --count prefix lines by the number of occurrences 去重后计数
2、实战演练
测试数据:
[root@web01 ~]# cat uniq.txt 10.0.0.9 10.0.0.8 10.0.0.7 10.0.0.7 10.0.0.8 10.0.0.8 10.0.0.9a、直接接文件,不加任何参数,只对相邻的相同内容去重:
[root@web01 ~]# uniq uniq.txt 10.0.0.9 10.0.0.8 10.0.0.7 10.0.0.8 10.0.0.9b、sort命令让重复的行相邻(-u参数也可完全去重),然后用uniq进行完全去重
[root@web01 ~]# sort uniq.txt 10.0.0.7 10.0.0.7 10.0.0.8 10.0.0.8 10.0.0.8 10.0.0.9 10.0.0.9 [root@web01 ~]# sort -u uniq.txt 10.0.0.7 10.0.0.8 10.0.0.9 [root@web01 ~]# sort uniq.txt|uniq 10.0.0.7 10.0.0.8 10.0.0.9c、sort配合uniq去重后计数
[root@web01 ~]# sort uniq.txt|uniq -c 2 10.0.0.7 3 10.0.0.8 2 10.0.0.93、企业案例
处理一下文件内容,将域名取出并根据域名进行计数排序处理(百度和sohu面试题)
[root@web01 ~]# cat access.log http://www.etiantian.org/index.html http://www.etiantian.org/1.html http://post.etiantian.org/index.html http://mp3.etiantian.org/index.html http://www.etiantian.org/3.html http://post.etiantian.org/2.html解答:
分析:此类问题是运维工作中最常见的问题。可以演变成分析日志,查看TCP各个状态连接数,查看单IP连接数排名等等。
[root@web01 ~]# awk -F '[/]+' '{print $2}' access.log|sort|uniq -c|sort -rn -k1 3 www.etiantian.org 2 post.etiantian.org 1 mp3.etiantian.org
本文出自 “万越天的架构师之路” 博客,请务必保留此出处http://wanyuetian.blog.51cto.com/3984643/1716971
相关文章推荐
- 第二节 BLE快速体验
- hadoop存储测试
- Java并发编程:线程池的使用
- CentOS下安装配置LAMP(Linux+Apache+MySQL+PHP)
- 玩转Highcharts图表库系列(二) 沿X轴设置不同的背景色分辨带
- 增加远程登录用户登陆个数
- Spring REST for DELETE Request Method Not Supoorted
- Android Fragment 真正的完全解析(上)
- 网站日志分析
- linux 常用命令
- Java算法---杨辉三角的打印输出
- SQLServer如何添加try catch
- iOS 快递查询
- Win2008R2PHP5.4环境加载Zend模块
- phpstrom xdebug wamp调试配置文档
- 高通平台点亮LCD个人总结
- Windows 10 TH2首个Cumulative Update发布,解决隐私设置问题
- Bootstrap CSS样式
- 修改 linux 默认启动级别 设置系统时间 定时关机
- 官村小学支教感言