TOP

grepとかawkとかテキスト操作系コマンドのメモ

2017-10-17 19:27:47

元ネタテキスト

テキストというかdocker ps

$ docker ps -a
CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS
7a2f54c497ba        Analyze:latest                 "/entrypoint.sh"         18 minutes ago      Up 18 minutes
8137dd971ed3        Analyze:latest                 "/entrypoint.sh"         25 hours ago        Exited (0) 25 hours ago
85522651013f        Analyze:latest                 "/entrypoint.sh"         25 hours ago        Exited (0) 25 hours ago
2ac628115aa4        Analyze:latest                 "/entrypoint.sh"         26 hours ago        Exited (0) 26 hours ago
0bd7d1970440        Analyze:latest                 "/entrypoint.sh"         26 hours ago        Exited (0) 26 hours ago
73479b05f1f8        nobiki/9zilla:latest           "/usr/bin/supervisord"   2 weeks ago         Up 2 hours
851ada8c938c        jenkins:2.19.2                 "/bin/tini -- /usr..."   2 weeks ago         Up 2 hours
94122d817559        nobiki/9zilla-repos-git:latest "/bootstrap.sh"          2 weeks ago         Up 2 hours

grep

// Exitedを含む(普通の使い方)
$ docker ps -a | grep "Exited"
8137dd971ed3        Analyze:latest                 "/entrypoint.sh"         25 hours ago        Exited (0) 25 hours ago
85522651013f        Analyze:latest                 "/entrypoint.sh"         25 hours ago        Exited (0) 25 hours ago
2ac628115aa4        Analyze:latest                 "/entrypoint.sh"         26 hours ago        Exited (0) 26 hours ago
0bd7d1970440        Analyze:latest                 "/entrypoint.sh"         26 hours ago        Exited (0) 26 hours ago

// 文字列を含まない物だけに絞る(-v)
$ docker ps -a | grep -v "Analyze"
CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS
73479b05f1f8        nobiki/9zilla:latest           "/usr/bin/supervisord"   2 weeks ago         Up 2 hours
851ada8c938c        jenkins:2.19.2                 "/bin/tini -- /usr..."   2 weeks ago         Up 2 hours
94122d817559        nobiki/9zilla-repos-git:latest "/bootstrap.sh"          2 weeks ago         Up 2 hours

// ヒット数カウント(-c)
$ docker ps -a | grep -c "Exited"
4

// ヒット数カウントに空文字を渡したら行数カウントになる
$ docker ps -a | grep -c ""
9

// 正規表現:「ago」で終わる
$ docker ps -a | grep "ago$"
8137dd971ed3        Analyze:latest                 "/entrypoint.sh"         25 hours ago        Exited (0) 25 hours ago
85522651013f        Analyze:latest                 "/entrypoint.sh"         25 hours ago        Exited (0) 25 hours ago
2ac628115aa4        Analyze:latest                 "/entrypoint.sh"         26 hours ago        Exited (0) 26 hours ago
0bd7d1970440        Analyze:latest                 "/entrypoint.sh"         26 hours ago        Exited (0) 26 hours ago

// 正規表現:「8」で始まる
$ docker ps -a | grep "^8"
8137dd971ed3        Analyze:latest                 "/entrypoint.sh"         25 hours ago        Exited (0) 25 hours ago
85522651013f        Analyze:latest                 "/entrypoint.sh"         25 hours ago        Exited (0) 25 hours ago
851ada8c938c        jenkins:2.19.2                 "/bin/tini -- /usr..."   2 weeks ago         Up 2 hours

// ヒットした行の「前後1行」を表示する(-1の部分に行数)
$ docker ps -a | grep -1 2ac628115aa4
85522651013f        Analyze:latest                 "/entrypoint.sh"         25 hours ago        Exited (0) 25 hours ago
2ac628115aa4        Analyze:latest                 "/entrypoint.sh"         26 hours ago        Exited (0) 26 hours ago
0bd7d1970440        Analyze:latest                 "/entrypoint.sh"         26 hours ago        Exited (0) 26 hours ago

// ヒットした行の「後1行」を表示する(1の部分に行数)
$ docker ps -a | grep -A 1 2ac628115aa4
2ac628115aa4        Analyze:latest                 "/entrypoint.sh"         26 hours ago        Exited (0) 26 hours ago
0bd7d1970440        Analyze:latest                 "/entrypoint.sh"         26 hours ago        Exited (0) 26 hours ago

// ヒットした行の「前1行」を表示する(1の部分に行数)
$ docker ps -a | grep -B 1 2ac628115aa4
85522651013f        Analyze:latest                 "/entrypoint.sh"         25 hours ago        Exited (0) 25 hours ago
2ac628115aa4        Analyze:latest                 "/entrypoint.sh"         26 hours ago        Exited (0) 26 hours ago

awk

// 「Exited」を含む
$ docker ps -a | awk '/Exited/'
8137dd971ed3        Analyze:latest                 "/entrypoint.sh"         25 hours ago        Exited (0) 25 hours ago
85522651013f        Analyze:latest                 "/entrypoint.sh"         25 hours ago        Exited (0) 25 hours ago
2ac628115aa4        Analyze:latest                 "/entrypoint.sh"         26 hours ago        Exited (0) 26 hours ago
0bd7d1970440        Analyze:latest                 "/entrypoint.sh"         26 hours ago        Exited (0) 26 hours ago

// 「Exited」を含む1カラム目を取り出す
$ docker ps -a | awk '/Exited/{print $1}'
8137dd971ed3
85522651013f
2ac628115aa4
0bd7d1970440

// 「Exited」を含むコンテナを一括破棄する(docker用)
$ docker ps -a | awk '/Exited/{print $1}' | xargs docker rm
8137dd971ed3
85522651013f
2ac628115aa4
0bd7d1970440

sed

// 一番よく使う置換
$ echo Hello World | sed -e 's/Hello/Konban/'
Konban World