选择行通过与条件格式化输出 [英] Select lines by condition with formatted output
本文介绍了选择行通过与条件格式化输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<一个href=\"http://stackoverflow.com/questions/36134001/select-lines-by-condition-and-count-with-one-line-command\">In另外我的问题
下面是例子日志:
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
10.10.10.10 - - [21 /三月/ 2016:00:00:00 +0000]GET /示例页面=安培; per_page = 100安培;范围= 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
使用下面的命令
AWK --re间隔'/ per_page = [0-9] {3} / {CNT [$ 1] ++} END {为(IP在CNT)打印IP, CNT [IP]}'文件
我可以得到数和每个电邮宣传女巫的IP cosist per_page结果>在参数= 100:
12.12.12.12 1
10.10.10.10 3
我怎么可以修改它与per_page参数值的输出?例如(任何形式):
12.12.12.12 - per_page-500 - 1
10.10.10.10 - per_page-100 - 2
10.10.10.10 - per_page-500 - 1
解决方案
AWK
来救援!
$ AWK --re间隔-v OFS =' - ''
比赛($ 0 / per_page = [0-9] {3} /){CNT [$ 1 OFS SUBSTR($ 0 RSTART,RLENGTH)]} +
END {为(以CNT IP)打印IP,CNT [IP]}'文件12.12.12.12 - per_page = 500 - 1
10.10.10.10 - per_page = 500 - 1
10.10.10.10 - per_page = 100 - 2
Here is example 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"
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"
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"
With following command
awk --re-interval '/per_page=[0-9]{3}/{cnt[$1]++} END{for (ip in cnt) print ip, cnt[ip]}' file
I can get counted and groupped result of each IPs witch cosist per_page >= 100 in parameters:
12.12.12.12 1
10.10.10.10 3
How I can modify it for output with per_page parameter value? For example (any format):
12.12.12.12 - per_page-500 - 1
10.10.10.10 - per_page-100 - 2
10.10.10.10 - per_page-500 - 1
解决方案
awk
to the rescue!
$ awk --re-interval -v OFS=' - ' '
match($0,/per_page=[0-9]{3}/){cnt[$1 OFS substr($0, RSTART,RLENGTH)]++}
END{for (ip in cnt) print ip, cnt[ip]}' file
12.12.12.12 - per_page=500 - 1
10.10.10.10 - per_page=500 - 1
10.10.10.10 - per_page=100 - 2
这篇关于选择行通过与条件格式化输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文