awk

1
2
3
4
5
6
7
8
9
10
11
12
13
// カンマ区切りの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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// ぼくのさいきょうの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
1
2
3
4
5
6
7
8
9
10
// 引数を任意の位置に挿入(-I を使う)
$ ls
moge1.txt moge2.txt moge3.txt
hoge1.txt hoge2.txt hoge3.txt

$ ls dest/
(※ここに mogeX.txt のみコピーする例)

// XXXの部分は任意。XXXがファイル名に置き換わる
$ ls | grep moge | xargs -IXXX cp XXX ./dest/

sed

超参考:sedコマンドで覚えておきたい使い方12個(+3個) | 俺的備忘録 〜なんかいろいろ〜

1
2
3
4
5
// 文字列置換
$ sed -ri "s/^#UsePAM no/UsePAM no/" /etc/ssh/sshd_config

// 3行目~5行目を表示
$ sed -n 3,5p [FILE]