TOP

awkとかxargsとかをそろそろ日常的に使っていきたい人のメモ

2017-04-27 17:40:14

awk

// カンマ区切りの3つ目取得
$ echo "111,222,333,444" | awk -F ',' '{print $3}'
333

// 末尾取得
$ echo "111,222,333,444" | awk -F ',' '{print $(NF)}'
444
$ echo "111,222,333,444" | awk -F ',' '{print $(NF-0)}'
444

// 末尾から1つ後ろ取得
$ echo "111,222,333,444" | awk -F ',' '{print $(NF-1)}'
333

xargs

// ぼくのさいきょうのDockerホスト
$ docker ps -a
CONTAINER ID        IMAGE                                 COMMAND                  CREATED             STATUS                      PORTS                                                             NAMES
f3f0ec388605        nobiki/9zilla-nginx-php:latest        "/bootstrap.sh"          5 weeks ago         Up 18 minutes                                                                                 9zilla-nginx-php
29c8ae533b3e        nobiki/9zilla-nginx-python:latest     "/bootstrap.sh"          5 weeks ago         Up 18 minutes                                                                                 9zilla-nginx-python
2f2753cb20b7        9zilla:1.2.4                          "/sbin/init"             5 weeks ago         Up 18 minutes               0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:19022->22/tcp   9zilla
a76740d93a40        nobiki/9zilla-lamp:latest             "/bootstrap.sh"          5 weeks ago         Up 18 minutes                                                                                 9zilla-lamp
326e4cd7154d        nobiki/9zilla-repos-git:latest        "/bootstrap.sh"          5 weeks ago         Up 18 minutes                                                                                 9zilla-repos-git
9f46d7b5b679        nobiki/9zilla-nginx-ruby:latest       "/bootstrap.sh"          5 weeks ago         Up 18 minutes                                                                                 9zilla-nginx-ruby
511f85b11ed3        jenkins:2.19.2                        "/bin/tini -- /usr..."   5 weeks ago         Exited (1) 18 minutes ago                                                                     ci-gandamu
13171e13547f        nobiki/9zilla-repos-composer:latest   "/bootstrap.sh"          5 weeks ago         Up 18 minutes                                                                                 9zilla-repos-composer
a4ca325616fd        redis:3.2.5                           "docker-entrypoint..."   5 weeks ago         Up 18 minutes               6379/tcp                                                          redis-gandamu
ce71042170cb        mysql:5.6                             "docker-entrypoint..."   5 weeks ago         Up 18 minutes               3306/tcp                                                          my-gandamu
770550dcd9b2        busybox                               "sh"                     5 weeks ago         Exited (0) 18 minutes ago                                                                     9zilla-volume-repository
3895f0be5233        busybox                               "sh"                     5 weeks ago         Exited (0) 18 minutes ago                                                                     9zilla-volume-contents
9abc0a84981c        nobiki/9zilla-volume-anyenv:latest    "/bin/bash"              5 weeks ago         Exited (0) 18 minutes ago                                                                     9zilla-volume-anyenv
c7ac51959f9c        busybox                               "sh"                     5 weeks ago         Exited (0) 18 minutes ago                                                                     9zilla-volume-database

// ステータスが「Exited」のやつだけgrepで絞りこみ
$ docker ps -a | grep "Exited"
511f85b11ed3        jenkins:2.19.2                        "/bin/tini -- /usr..."   5 weeks ago         Exited (1) 19 minutes ago                                                                     ci-gandamu
770550dcd9b2        busybox                               "sh"                     5 weeks ago         Exited (0) 19 minutes ago                                                                     9zilla-volume-repository
3895f0be5233        busybox                               "sh"                     5 weeks ago         Exited (0) 19 minutes ago                                                                     9zilla-volume-contents
9abc0a84981c        nobiki/9zilla-volume-anyenv:latest    "/bin/bash"              5 weeks ago         Exited (0) 19 minutes ago                                                                     9zilla-volume-anyenv
c7ac51959f9c        busybox                               "sh"                     5 weeks ago         Exited (0) 19 minutes ago                                                                     9zilla-volume-database

// 「awk」でコンテナ名だけ抜き出す
$ docker ps -a | grep "Exited" | awk -F ' ' '{print $(NF)}'
ci-gandamu
9zilla-volume-repository
9zilla-volume-contents
9zilla-volume-anyenv
9zilla-volume-database

// 「xargs docker start」にパイプして全部start
$ docker ps -a | grep "Exited" | awk -F ' ' '{print $(NF)}' | xargs docker start
ci-gandamu
9zilla-volume-repository
9zilla-volume-contents
9zilla-volume-anyenv
9zilla-volume-database