内容简介:test.log日志列数比较少或则要提取的字段比较靠前时,优先使用awk。当然cut也可以做到。比如输出日志的第三列
test.log
2019-1010-1920 192.345.23.3 cause:"AAA" type:"A" loginIn 2019-1010-1920 192.345.23.1 cause:"BBB" type:"A" loginIn 2019-1010-1920 192.345.23.3 cause:"AAA" type:"S" loginIn 2019-1010-1920 192.345.23.1 cause:"BBJ" type:"A" loginIn
按列分割
提取第三列
日志列数比较少或则要提取的字段比较靠前时,优先使用awk。当然cut也可以做到。
比如输出日志的第三列
awk '{print $3}' test.log // $3表示第三列
cut -d " " -f3 test.log // -f3指定第三列, -d用来指定分割符
正则提取
提取cause字段的原因值?
2019-1010-1920 192.345.23.3 cause:"AAA" type:"A" loginIn 2019-1010-1920 192.345.23.1 type:"A" loginIn cause:"BBB" 2019-1010-1920 192.345.23.3 cause:"AAA" type:"S" loginIn 2019-1010-1920 192.345.23.1 type:"A" cause:"BBJ" loginIn
当要提取的内容不在同一列时,往往就无法用cut或者awk就按列提取
。最好用的方式是用grep的正则提取。
好像grep不支持捕获分组,所以只能提取出出cause:”AAA”,而无法直接提取出AAA
- E 表示使用正则
- o 表示只显示匹配到的内容
> grep -Eo 'cause:".*?"' test.log cause:"AAA" cause:"BBB" cause:"AAA" cause:"BBJ"
统计
对输出的关键词进行统计,并按照升序或者降序排列。
将关键词按照列或者按照正则提取出来之后,首先要进行 sort
排序, 然后再进行 uniq
去重。
不进行 排序 就直接去重,统计的值就不准确。因为uniq去重只能去除连续的相同字符串。不是连续的字符串,则会统计多次。
下面例子:非连续的cause:”AAA”,没有被合并在一起计数
// bad grep -Eo 'cause:".*?"' test.log | uniq -c 1 cause:"AAA" 1 cause:"BBB" 1 cause:"AAA" 1 cause:"BBJ" // good AAA 被正确统计了 grep -Eo 'cause:".*?"' test.log | sort | uniq -c 2 cause:"AAA" 1 cause:"BBB" 1 cause:"BBJ"
对统计值排序
sort默认的排序是按照字典排序, 可以使用-n参数让其按照数值大小排序。
-r
// 升序排序 grep -Eo 'cause:".*?"' test.log | sort |uniq -c | sort -n 1 cause:"BBB" 1 cause:"BBJ" 2 cause:"AAA" // 降序排序 grep -Eo 'cause:".*?"' test.log | sort |uniq -c | sort -nr 2 cause:"AAA" 1 cause:"BBJ" 1 cause:"BBB"
以上所述就是小编给大家介绍的《awk、grep、cut、sort、uniq简单命令玩转日志分析与统计》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- Linux time 命令统计脚本耗时
- 在命令行进行简单的统计分析
- Nginx Access Log日志统计分析常用命令
- 微信小程序数据统计和错误统计的实现
- 机器学习数学基础:数理统计与描述性统计
- Java8中使用stream进行分组统计和普通实现的分组统计的性能对比
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Machine Learning
Kevin Murphy / The MIT Press / 2012-9-18 / USD 90.00
Today's Web-enabled deluge of electronic data calls for automated methods of data analysis. Machine learning provides these, developing methods that can automatically detect patterns in data and then ......一起来看看 《Machine Learning》 这本书的介绍吧!