awkの連想配列を用いてカウントを取る
はじめに
awk
の連想配列を覚えると、出来ることの幅が広がる気がしたので、その紹介です。
awk
すげーってなったら良いなと思って書きました。
適当なファイルを用意してawk
で全量出力する。
コマンド
$ awk '{print $0;}' test.txt
結果
192.168.0.1 - - [5/Dec/2017:21:00:00 +0000] "GET /index.html HTTP/1.1" 192.168.0.1 - - [5/Dec/2017:21:01:00 +0000] "GET /index.html HTTP/1.1" 192.168.0.1 - - [5/Dec/2017:21:02:00 +0000] "GET /index.html HTTP/1.1" 192.168.0.1 - - [5/Dec/2017:21:00:00 +0000] "GET /index2.html HTTP/1.1" 192.168.0.1 - - [5/Dec/2017:21:01:00 +0000] "GET /index2.html HTTP/1.1" 192.168.0.1 - - [5/Dec/2017:21:02:00 +0000] "GET /index2.html HTTP/1.1"
半角スペース区切りで7列目に存在するリソース毎にカウントを取る。
コマンド
$ awk '{request[$7]++;}END{for(key in request){print key, request[key];}}' test.txt
結果
/index2.html 3 /index.html 3
半角スペースが連続している場合
(区切り文字無しでパラメータhogeを出力するフォーマットがいけてない。)
$ cat test2.txt 192.168.0.1 - - [5/Dec/2017:21:00:00 +0000] "GET /index.html HTTP/1.1" 192.168.0.1 - - [5/Dec/2017:21:01:00 +0000] "GET /index.html HTTP/1.1" 192.168.0.1 - - [5/Dec/2017:21:02:00 +0000] "GET /index.html HTTP/1.1" 192.168.0.1 - - [5/Dec/2017:21:00:00 +0000] "GET /index2.html HTTP/1.1" 192.168.0.1 - - [5/Dec/2017:21:01:00 +0000] "GET hoge /index2.html HTTP/1.1" 192.168.0.1 - - [5/Dec/2017:21:02:00 +0000] "GET /index2.html HTTP/1.1"
BEGIN{FS="[ ]"}
を追加し、連続した半角スペースをそれぞれ区切り文字と認識させる。リソース名は8列目になる。
コマンド
$ awk 'BEGIN{FS="[ ]"}{request[$8]++;}END{for(key in request){print key, request[key];}}' test2.txt
結果
/index2.html 3 /index.html 3
時間あたりのリクエスト数を取る
このような連想配列を作るイメージ
request[/index.html 21:00]++ request[/index2.html 21:00]++
コマンド
$ awk 'BEGIN{FS="[ ]"}{request[substr($4, 13, 2) ":00 " $8]++;}END{for(key in request){print key, request[key];}}' test2.txt
結果
21:00 /index2.html 3 21:00 /index.html 3
GradleをMacへインストールする
Gradleが気になるので、実行できる状態を準備する。 ゆくゆくはGradleのタスクを実行するだけで、IDEでコンパイル可能なプロジェクトを準備するところまで実現したい。
MacへSDKMAN!をインストールする
Groovy環境マネージャーのGVM(Groovy enVironment Manager)がSDKMAN!へ進化した様子。
SDKMAN!を参考にcurl
を叩く。
$ curl -s "https://get.sdkman.io" | bash ※SDK!とAAで表示 Now attempting installation... Looking for a previous installation of SDKMAN... Looking for unzip... Looking for zip... Looking for curl... Looking for sed... Installing SDKMAN scripts... Create distribution directories... Getting available candidates... Prime the config file... Download script archive... ######################################################################## 100.0% Extract script archive... Install scripts... Set version to 5.5.13+272 ... Attempt update of interactive bash profile... Added sdkman init snippet to /Users/ユーザ名/.bashrc Attempt update of zsh profile... Updated existing /Users/ユーザ名/.zshrc All done! Please open a new terminal, or run the following in the existing one: source "/Users/ユーザ名/.sdkman/bin/sdkman-init.sh" Then issue the following command: sdk help Enjoy!!! $ source "/Users/ユーザ名/.sdkman/bin/sdkman-init.sh" $ sdk version SDKMAN 5.5.13+272
SDKMAN!を使用してMacへGradleをインストールする
GradleのInstallationを参考に、sdk
コマンドを実行する。
$ java -version java version "1.8.0_73"
Java、古いね。
$ sdk install gradle 4.3.1 Downloading: gradle 4.3.1 In progress... ######################################################################## 100.0% Installing: gradle 4.3.1 Done installing! Setting gradle 4.3.1 as default. $ gradle -v ------------------------------------------------------------ Gradle 4.3.1 ------------------------------------------------------------ Build time: 2017-11-08 08:59:45 UTC Revision: e4f4804807ef7c2829da51877861ff06e07e006d Groovy: 2.4.12 Ant: Apache Ant(TM) version 1.9.6 compiled on June 29 2015 JVM: 1.8.0_73 (Oracle Corporation 25.73-b02) OS: Mac OS X 10.11.6 x86_64
If your existing Gradle-based build uses the Gradle Wrapper, you can easily upgrade by running the wrapper task, specifying the desired Gradle version:
既存のGradleベースのビルドでGradle Wrapperを使用している場合は、目的のGradleバージョンを指定してラッパータスクを実行して簡単にアップグレードできます。
使用していないのでスキップする。
はじめてのGitHub
GitHubにリポジトリを作成してローカルのソースをコミットするまでの履歴を残す。
GitHub作業
https://github.com/
GitHubのアカウントを作成。
※2014年にアカウントは作成済みだった。
javaee7studyリポジトリを作成。
ローカル作業
Eclipseのワークスペースへ移動
GitHubのリポジトリをローカルへ持ってくる。
$ git clone https://github.com/ikhr/javaee7study Cloning into 'javaee7study'... remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. $ cd javaee7study/
試しに作ったソースコードをjavaee7studyフォルダへコピーしGitHubへ反映。
$ git status On branch master Your branch is up-to-date with 'origin/master'. Untracked files: (use "git add <file>..." to include in what will be committed) pom.xml src/ nothing added to commit but untracked files present (use "git add" to track) $ git add . $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: pom.xml new file: src/main/java/com/app/TodoBean.java new file: src/main/java/com/app/flow/FlowCounter.java new file: src/main/java/com/app/flow/FlowCounterDefinition.java new file: src/main/webapp/WEB-INF/web.xml new file: src/main/webapp/flow/firstFlow.xhtml new file: src/main/webapp/flow/flowEntry.xhtml new file: src/main/webapp/flow/secondFlow.xhtml new file: src/main/webapp/flow/thirdFlow.xhtml new file: src/main/webapp/todo.xhtml $ git branch * master $ git branch -a * master remotes/origin/HEAD -> origin/master remotes/origin/master $ git commit -m "お試し初回コミット" [master e4e67a3] お試し初回コミット Committer: PCのユーザ名 <PCのマシン名> Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly. Run the following command and follow the instructions in your editor to edit your configuration file: git config --global --edit After doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 10 files changed, 326 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/com/app/TodoBean.java create mode 100644 src/main/java/com/app/flow/FlowCounter.java create mode 100644 src/main/java/com/app/flow/FlowCounterDefinition.java create mode 100644 src/main/webapp/WEB-INF/web.xml create mode 100644 src/main/webapp/flow/firstFlow.xhtml create mode 100644 src/main/webapp/flow/flowEntry.xhtml create mode 100644 src/main/webapp/flow/secondFlow.xhtml create mode 100644 src/main/webapp/flow/thirdFlow.xhtml create mode 100644 src/main/webapp/todo.xhtml $ git push Counting objects: 21, done. Delta compression using up to 4 threads. Compressing objects: 100% (17/17), done. Writing objects: 100% (21/21), 5.29 KiB | 0 bytes/s, done. Total 21 (delta 2), reused 0 (delta 0) remote: Resolving deltas: 100% (2/2), done. To https://github.com/ikhr/javaee7study 5bf247d..e4e67a3 master -> master
GitHub作業
GitHubでコミットを確認。
コミットユーザがフルネームになっていた。
ローカル作業
コミットユーザ名を変更する。
$ git config --local user.name ikhr $ git config --local user.email gat.d90@gmail.com $ git commit --amend --author="ikhr <gat.d90@gmail.com>" [master 1c6dd5a] お試し初回コミット Date: Sat Dec 2 11:43:08 2017 +0900 10 files changed, 326 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/com/app/TodoBean.java create mode 100644 src/main/java/com/app/flow/FlowCounter.java create mode 100644 src/main/java/com/app/flow/FlowCounterDefinition.java create mode 100644 src/main/webapp/WEB-INF/web.xml create mode 100644 src/main/webapp/flow/firstFlow.xhtml create mode 100644 src/main/webapp/flow/flowEntry.xhtml create mode 100644 src/main/webapp/flow/secondFlow.xhtml create mode 100644 src/main/webapp/flow/thirdFlow.xhtml create mode 100644 src/main/webapp/todo.xhtml $ git rebase --continue No rebase in progress? $ git log --pretty=full commit 1c6dd5ad6a1aaefd5c395876196a6a33536ebbc7 Author: ikhr <gat.d90@gmail.com> Commit: ikhr <gat.d90@gmail.com> お試し初回コミット commit 5bf247daddb833b335bcd6140d4e40e679c1353e Author: ikhr <gat.d90@gmail.com> Commit: GitHub <noreply@github.com> Initial commit $ git push --force-with-lease origin master Counting objects: 21, done. Delta compression using up to 4 threads. Compressing objects: 100% (17/17), done. Writing objects: 100% (21/21), 5.26 KiB | 0 bytes/s, done. Total 21 (delta 2), reused 0 (delta 0) remote: Resolving deltas: 100% (2/2), done. To https://github.com/ikhr/javaee7study + e4e67a3...1c6dd5a master -> master (forced update)
GitHub作業
GitHubでコミットを確認。 コミットユーザが ikhr になった。
2017.12.6 追記
ユーザ名とメールアドレスをglobal
で登録しなおした。
$ git config --global user.name ikhr $ git config --global user.email gat.d90@gmail.com $ git config --list credential.helper=osxkeychain user.name=ikhr user.email=gat.d90@gmail.com
2017.12.10 追記
git
とbrew
は奥さんがインストールを済ませていた雰囲気。