美文网首页
linux grep awk 匹配数据

linux grep awk 匹配数据

作者: 木子李_af14 | 来源:发表于2017-12-20 21:02 被阅读149次

利用grep的-oE获取数据

  1. 本地mock一份数据用于测试
2017-12-20 20:00:00 mobile=123,err=MOBILE_ERROR.
2017-12-20 20:01:00 mobile=124,err=MOBILE_ERROR.
2017-12-20 20:02:00 mobile=125,err=MOBILE_ERROR.
2017-12-20 20:03:00 mobile=1236,err=MOBILE_ERROR.
2017-12-20 20:04:00 mobile=127,err=MOBILE_ERROR.
2017-12-20 20:05:00 mobile=128,err=MOBILE_ERROR.
2017-12-20 20:06:00 mobile=129,err=MOBILE_ERROR.
2017-12-20 20:07:00 mobile=120,err=MOBILE_ERROR.
2017-12-20 20:08:00 mobile=11,err=MOBILE_ERROR.
2017-12-20 20:09:00 mobile=123,err=MOBILE_ERROR.
2017-12-20 20:10:00 mobile=133,err=MOBILE_ERROR.
2017-12-20 20:11:00 mobile=143,err=MOBILE_ERROR.
2017-12-20 20:12:00 mobile=153,err=MOBILE_ERROR.
2017-12-20 20:13:00 mobile=163,err=MOBILE_ERROR.
2017-12-20 20:14:00 mobile=173,err=MOBILE_ERROR.
2017-12-20 20:15:00 mobile=183,err=MOBILE_ERROR.
2017-12-20 20:16:00 mobile=193,err=MOBILE_ERROR.
2017-12-20 20:17:00 mobile=223,err=MOBILE_ERROR.
2017-12-20 20:18:00 mobile=323,err=MOBILE_ERROR.
2017-12-20 20:19:00 mobile=423,err=MOBILE_ERROR.
2017-12-20 20:20:00 mobile=523,err=MOBILE_ERROR.
2017-12-20 20:21:00 mobile=623,err=MOBILE_ERROR.
2017-12-20 20:22:00 mobile=723,err=MOBILE_ERROR.
  1. 实际操作
xuxinbin@LAPTOP-B8DEROE0 ~/testDir
$ grep 'MOBILE_ERROR' grep.log | grep '^2017-12-20 20:0' --col
2017-12-20 20:00:00 mobile=123,err=MOBILE_ERROR.
2017-12-20 20:01:00 mobile=124,err=MOBILE_ERROR.
2017-12-20 20:02:00 mobile=125,err=MOBILE_ERROR.
2017-12-20 20:03:00 mobile=1236,err=MOBILE_ERROR.
2017-12-20 20:04:00 mobile=127,err=MOBILE_ERROR.
2017-12-20 20:05:00 mobile=128,err=MOBILE_ERROR.
2017-12-20 20:06:00 mobile=129,err=MOBILE_ERROR.
2017-12-20 20:07:00 mobile=120,err=MOBILE_ERROR.
2017-12-20 20:08:00 mobile=11,err=MOBILE_ERROR.
2017-12-20 20:09:00 mobile=123,err=MOBILE_ERROR.

xuxinbin@LAPTOP-B8DEROE0 ~/testDir
$ grep 'MOBILE_ERROR' grep.log | grep '^2017-12-20 20:0' --col | grep -oE 'mobile'
mobile
mobile
mobile
mobile
mobile
mobile
mobile
mobile
mobile
mobile

xuxinbin@LAPTOP-B8DEROE0 ~/testDir
$ grep 'MOBILE_ERROR' grep.log | grep '^2017-12-20 20:0' --col | grep -oE 'mobile=[0-9]+'
mobile=123
mobile=124
mobile=125
mobile=1236
mobile=127
mobile=128
mobile=129
mobile=120
mobile=11
mobile=123

xuxinbin@LAPTOP-B8DEROE0 ~/testDir
$ grep 'MOBILE_ERROR' grep.log | grep '^2017-12-20 20:0' --col | grep -oE 'mobile=[0-9]+' | grep -oE '[0-9]+'
123
124
125
1236
127
128
129
120
11
123

利用grep awk uniq sort统计数据

实例一

  1. mock的数据
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统1,queryName,,SYNC,IP地址,被调用系统1,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统1,queryName,,SYNC,IP地址,被调用系统1,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统2,queryName,,SYNC,IP地址,被调用系统2,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统3,queryName,,SYNC,IP地址,被调用系统3,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统1,queryName,,SYNC,IP地址,被调用系统1,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统4,queryName,,SYNC,IP地址,被调用系统4,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统5,queryName,,SYNC,IP地址,被调用系统5,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统1,queryName,,SYNC,IP地址,被调用系统1,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统2,queryName,,SYNC,IP地址,被调用系统2,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统3,queryName,,SYNC,IP地址,被调用系统3,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统4,queryName,,SYNC,IP地址,被调用系统4,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统4,queryName,,SYNC,IP地址,被调用系统4,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统4,queryName,,SYNC,IP地址,被调用系统4,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统4,queryName,,SYNC,IP地址,被调用系统4,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统4,queryName,,SYNC,IP地址,被调用系统4,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统5,queryName,,SYNC,IP地址,被调用系统5,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统1,queryName,,SYNC,IP地址,被调用系统1,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统1,queryName,,SYNC,IP地址,被调用系统1,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统1,queryName,,SYNC,IP地址,被调用系统1,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统1,queryName,,SYNC,IP地址,被调用系统1,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,
2017-12-25 17:17:24.014,sys1,0bef5b2c1514193444008270345056,0,com.java.QueryFacade:1.0:被调用系统1,queryName,,SYNC,IP地址,被调用系统1,域名,,,,00,513B,6618B,6ms,0ms,0ms,6ms,线程名-7,CFS,,F,,客户端IP地址,域名,,

  1. 实际操作
xuxinbin@LAPTOP-B8DEROE0 ~/testDir
$ grep '17:[0-9][0-9]:' awk.sh | awk -F, '{if(int($19)>=0)print $10}' | sort | uniq -c | sort -n
      2 被调用系统2
      2 被调用系统3
      2 被调用系统5
      6 被调用系统4
      9 被调用系统1

  1. 命令解析
    grep根据正则匹配出来的数据,通过管道符,进行编辑
    -F, 表示根据逗号分割
    '' 表示执行命令
    $19 其实是表示根据逗号分隔后,第19个参数,如果大于等于0,那么输出第10个参数$10
    sort 先排序
    uniq -c 统计单词出现的个数(如果不先sort,uniq统计的时候,不会遍历所有文本,只会向下统计,遇到不一样的单词就停下了)
    sort -n 就是numeric sort,根据数字来统计

实例二

  1. 数据mock
3 0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  226610000679669213
62  10.000000 2.000000  0.000000  0.000000  0.000000  0.000000  226610000383532025
64  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  226610000362182755
68  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  226610000253021042
69  30.000000 0.000000  0.000000  0.000000  0.000000  0.000000  226610000447631031
102 0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  226610000457382907
115 0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  226610000176391352
118 0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  226610000375507561
134 0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  226610000457315642
146 0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  226610000370384291

153 4.200000  0.000000  0.000000  0.000000  0.000000  0.000000  226610000469858179
158 0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  226610000095392984
164 0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  226610000309891100
169 0.100000  0.000000  0.000000  0.000000  0.000000  0.000000  226610000459825457
180 0.004000  0.000000  0.000000  0.000000  0.000000  0.000000  226610000371417886
187 0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  226610000187225082
194 2.000000  3.000000  0.000000  0.000000  0.000000  0.000000  226610000172856224
198 0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  226610000055941322
199 6.000000  0.000000  0.000000  0.000000  0.000000  0.000000  226610000459828661
205 10.000000 0.000000  0.000000  0.000000  0.000000  0.000000  226610000467887955
  1. 累加统计第一列的值,并打印
$ awk -F' ' '{sum += $1} END {print sum}' temp.txt
2688

相关文章

网友评论

      本文标题:linux grep awk 匹配数据

      本文链接:https://www.haomeiwen.com/subject/addnwxtx.html