Return to Tech

Awk

Examples
NF-1: 後列から2番目にHTTPレスポンスステータスコードが記録されています。

$ awk '{print $(NF-1)}' /var/somelocation/kshell-access_log* | sort | uniq -c
 22590 200
     2 201
     2 204
   124 206
    27 304
   211 400
  1724 404
   771 408
    15 503
列数により記録内容が異なるので調査・分析

$ cat /var/somelocation/httpd/kshell-access_log* | awk '{print NF}' | sort | uniq -c
 24674 11
     1 13
     1 14
     2 15
     1 16
     1 18
   830  9
?含む後続を省略する例
※グループ化・絞り込みの際に利用することを想定

$ echo 'GET /api/v1?k=1' | awk '{printf("%s %s\n", $1, $2)}'
GET /api/v1?k=1

$ echo 'GET /api/v1?k=1' | awk '{printf("%s %s\n", $1, substr($2, 0, index($2, "?")-1) )}'
GET /api/v1
Unique Urls
Filter By StatusCode
Summarize Status Codes

Return to Tech