通过选择条件线和一个命令行的数 [英] Select lines by condition and count with one line command

查看:97
本文介绍了通过选择条件线和一个命令行的数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助分析n​​ginx的日志。日志示例:

  10.10.10.10  -   -  [21 /三月/ 2016:00:00:00 +0000]GET /示例页面=安培; per_page = 100安培;范围= HTTP / 1.1吗? 200 769 - 1.1.1.1
10.10.10.10 - - [21 /三月/ 2016:00:00:00 +0000]GET /示例页面=安培; per_page = 500安培;范围= HTTP / 1.1?200 769 - 1.1.1.1
11.11.11.11 - - [21 /三月/ 2016:00:00:00 +0000]GET /示例页面=安培; per_page = 10安培;范围= HTTP / 1.1?200 769 - 1.1.1.1
12.12.12.12 - - [21 /三月/ 2016:00:00:00 +0000]GET /示例页面=安培; per_page = 500安培;范围= HTTP / 1.1?200 769 - 1.1.1.1
13.13.13.13 - - [21 /三月/ 2016:00:00:00 +0000]GET /例如HTTP / 1.1200 769 - 1.1.1.1

是否有可能与计数选择含per_page参数,这个参数等于或大于100 uniq的所有IP地址?

因此​​,输出可以是任何格式:

  10.10.10.10  -  2#IP 10.10.10.10被发现两次
12.12.12.12 - 1

是否可以用一个命令来获得?


解决方案

  $ awk的'/ per_page = [0-9] {3} / {CNT [$ 1] ++} END {为(以CNT IP)打印IP,CNT [IP]}'文件
12.12.12.12 1
10.10.10.10 2

这绝对是基本的AWK - 读的书有效AWK编程,第4版,由阿诺德·罗宾斯,如果你要在UNIX其他任何文本文件处理

I need help with analyze nginx logs. Sample of log:

10.10.10.10 - - [21/Mar/2016:00:00:00 +0000] "GET /example?page=&per_page=100&scopes= HTTP/1.1" 200 769 "-" "" "1.1.1.1"
10.10.10.10 - - [21/Mar/2016:00:00:00 +0000] "GET /example?page=&per_page=500&scopes= HTTP/1.1" 200 769 "-" "" "1.1.1.1"
11.11.11.11 - - [21/Mar/2016:00:00:00 +0000] "GET /example?page=&per_page=10&scopes= HTTP/1.1" 200 769 "-" "" "1.1.1.1"
12.12.12.12 - - [21/Mar/2016:00:00:00 +0000] "GET /example?page=&per_page=500&scopes= HTTP/1.1" 200 769 "-" "" "1.1.1.1"
13.13.13.13 - - [21/Mar/2016:00:00:00 +0000] "GET /example HTTP/1.1" 200 769 "-" "" "1.1.1.1"

Is it possible to select with count all uniq ip addresses which contain per_page parameter and this parameter equal or greater than 100?

So, the output can be in any format:

10.10.10.10 - 2 # ip 10.10.10.10 was found twice
12.12.12.12 - 1

Is it possible to get with one command?

解决方案

$ awk '/per_page=[0-9]{3}/{cnt[$1]++} END{for (ip in cnt) print ip, cnt[ip]}' file
12.12.12.12 1
10.10.10.10 2

This is absolutely basic awk - read the book Effective Awk Programming, 4th Edition, by Arnold Robbins if you're going to be any other text file processing in UNIX.

这篇关于通过选择条件线和一个命令行的数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆