ShellでHTTPRequestログを精査 4
ShellでHTTPRequestログを精査 1 - xai1981's blog
shell スクリプトでファイルから1行ずつ読み込んだデータを正規表現を使って判断する - xai1981's blog
ShellでHTTPRequestログを精査 3 - xai1981's blog
上記の続き~ って事で今回で最後です。 input ディレクトリからファイルを読み込んで、特定の行の特定の箇所を抽出し output ディレクトリに書き出します。やってみて分かりました、テキスト操作はとんでもなく時間がかかりますね。なので、現在実行中のファイル名と行数を出力するようにしています。
input ディレクトリから 1ファイル・1行ずつ読み込んで正規表現を使って欲しい情報のみを output ディレクトリに出力するスクリプト
[root@kabosu ~/work]$ cat readline_one_expr_file_cat_and_write.sh #!/bin/sh for current_file in `ls input` do echo $current_file cnt=1 cat "input/${current_file}" | while read line do echo $cnt $current_file if expr "${line}" : ".*POST /user/[a-zA-Z]\+ .*"; then echo ${line} | sed -e "s/^.*POST \/user\/\([a-zA-Z]\+\).*$/POST \/user\/\1/" >> "output/${current_file}" fi let cnt=${cnt}+1 done done
テスト用の読み込みアクセスログ1と2
[root@kabosu ~/work]$ cat input/access.log-20100101 www.hoge.com 1.1.1.1 - - "POST /user/AAAAA HTTP/1.1" 200 61 ... www.hoge.com 1.1.1.1 - - "POST /user/BBBBB HTTP/1.1" 200 61 ... www.hoge.com 1.1.1.1 - - "GET /hoge HTTP/1.1" 200 9570 ... www.hoge.com 1.1.1.1 - - "POST /user/CCCCC HTTP/1.1" 200 61 ... www.hoge.com 1.1.1.1 - - "GET /photo HTTP/1.1" 302 5 ... www.hoge.com 1.1.1.1 - - "POST /user/DDDDD HTTP/1.1" 200 61 ... www.hoge.com 1.1.1.1 - - "POST /user/EEEEE HTTP/1.1" 200 61 ... www.hoge.com 1.1.1.1 - - "GET /bar HTTP/1.1" 200 779 ... www.hoge.com 1.1.1.1 - - "POST /foo HTTP/1.1" 200 313 ... www.hoge.com 1.1.1.1 - - "GET / HTTP/1.1" 200 20912 ... www.hoge.com 1.1.1.1 - - "GET /hoge HTTP/1.1" 302 5 ... www.hoge.com 1.1.1.1 - - "GET /video/100 HTTP/1.1" 200 74 ... www.hoge.com 1.1.1.1 - - "GET /video/100 HTTP/1.1" 200 74 ...
[root@kabosu ~/work]$ cat input/access.log-20100102 www.hoge.com 1.1.1.1 - - "POST /user/aaaaa HTTP/1.1" 200 61 ... www.hoge.com 1.1.1.1 - - "POST /user/bbbbb HTTP/1.1" 200 61 ... www.hoge.com 1.1.1.1 - - "GET /hoge HTTP/1.1" 200 9570 ... www.hoge.com 1.1.1.1 - - "POST /user/ccccc HTTP/1.1" 200 61 ... www.hoge.com 1.1.1.1 - - "GET /photo HTTP/1.1" 302 5 ... www.hoge.com 1.1.1.1 - - "POST /user/ddddd HTTP/1.1" 200 61 ... www.hoge.com 1.1.1.1 - - "POST /user/eeeee HTTP/1.1" 200 61 ... www.hoge.com 1.1.1.1 - - "GET /bar HTTP/1.1" 200 779 ... www.hoge.com 1.1.1.1 - - "POST /foo HTTP/1.1" 200 313 ... www.hoge.com 1.1.1.1 - - "GET / HTTP/1.1" 200 20912 ... www.hoge.com 1.1.1.1 - - "GET /hoge HTTP/1.1" 302 5 ... www.hoge.com 1.1.1.1 - - "GET /video/100 HTTP/1.1" 200 74 ... www.hoge.com 1.1.1.1 - - "GET /video/100 HTTP/1.1" 200 74 ...
POST /user/[a-zA-Z]+ のみを抽出して output 配下のファイルに書き込みます。
実行結果
[root@kabosu ~/work]$ ./readline_one_expr_file_cat_and_write.sh access.log-20100101 1 access.log-20100101 63 2 access.log-20100101 63 3 access.log-20100101 0 4 access.log-20100101 63 5 access.log-20100101 0 6 access.log-20100101 63 7 access.log-20100101 63 8 access.log-20100101 0 9 access.log-20100101 0 10 access.log-20100101 0 11 access.log-20100101 0 12 access.log-20100101 0 13 access.log-20100101 0 access.log-20100102 1 access.log-20100102 63 2 access.log-20100102 63 3 access.log-20100102 0 4 access.log-20100102 63 5 access.log-20100102 0 6 access.log-20100102 63 7 access.log-20100102 63 8 access.log-20100102 0 9 access.log-20100102 0 10 access.log-20100102 0 11 access.log-20100102 0 12 access.log-20100102 0 13 access.log-20100102 0
書き出したファイル1と2
[root@kabosu ~/work]$ cat output/access.log-20100101 POST /user/AAAAA POST /user/BBBBB POST /user/CCCCC POST /user/DDDDD POST /user/EEEEE
[root@kabosu ~/work]$ cat output/access.log-20100102 POST /user/aaaaa POST /user/bbbbb POST /user/ccccc POST /user/ddddd POST /user/eeeee
これでやりたい事はできました。 shell の シングルコーテーション、ダブルコーテーション、バッククオートの使い方、など分かってない所が多々あるので、別の機会に再度復習しようと思います ^^