awkの正規表現でIPアドレスを含む行のみ処理させる
Pocket

awkで、正規表現を使ってIPアドレスを含む行のみ処理をさせるには、以下のようにコマンドを実行すればよい。

awk 'match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/) {処理内容}'
Sponsored Links

blacknon@BS-PUB-UBUNTU-01:~$ cat /tmp/test3.txt
wqrg eth0
192.168.0.11
00:0d:b9:35:b2:d1 192.168.0.1
00:15:17:bd:d6:3e 192.168.0.10
bc:5f:f4:1a:6e:6c 192.168.0.12
36:62:38:64:38:63 192.168.0.20
38:ea:a7:a4:a5:10 192.168.0.30
62:66:39:65:63:39 192.168.0.51
62:38:66:39:34:39 192.168.0.52
32:36:34:63:63:61 192.168.0.53
36:38:33:33:63:36 192.168.0.54
32:32:64:37:39:64 192.168.0.55
32:66:34:61:34:65 192.168.0.57
aaaaaa
66:32:64:38:66:37 192.168.0.58
62:31:30:31:39:64 192.168.0.102
32:33:61:62:39:61 192.168.0.103
66:62:30:61:35:35 192.168.0.114
ffffff
32:65:37:36:64:61 192.168.0.117
62:34:30:62:31:62 192.168.0.118
32:64:35:36:37:38 192.168.0.119
36:33:62:65:63:36 192.168.0.121
62:64:62:31:63:62 192.168.0.145
36:35:64:66:35:61 192.168.0.169
62:66:33:31:64:34 192.168.0.170
32:33:35:31:37:33 192.168.0.171
32:36:63:39:36:37 192.168.0.175
66:63:32:30:35:65 192.168.0.176
62:30:34:62:63:32 192.168.0.177
32:34:33:65:35:32 192.168.0.178
32:34:33:65:25:32 192.168.0.178
66:66:63:65:34:36 192.168.0.190
62:30:34:62:63:32 192.168.0.231
sasasa
blacknon@BS-PUB-UBUNTU-01:~$ cat /tmp/test3.txt | awk 'match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/) {print $2,$1}'
 192.168.0.11
192.168.0.1 00:0d:b9:35:b2:d1
192.168.0.10 00:15:17:bd:d6:3e
192.168.0.12 bc:5f:f4:1a:6e:6c
192.168.0.20 36:62:38:64:38:63
192.168.0.30 38:ea:a7:a4:a5:10
192.168.0.51 62:66:39:65:63:39
192.168.0.52 62:38:66:39:34:39
192.168.0.53 32:36:34:63:63:61
192.168.0.54 36:38:33:33:63:36
192.168.0.55 32:32:64:37:39:64
192.168.0.57 32:66:34:61:34:65
192.168.0.58 66:32:64:38:66:37
192.168.0.102 62:31:30:31:39:64
192.168.0.103 32:33:61:62:39:61
192.168.0.114 66:62:30:61:35:35
192.168.0.117 32:65:37:36:64:61
192.168.0.118 62:34:30:62:31:62
192.168.0.119 32:64:35:36:37:38
192.168.0.121 36:33:62:65:63:36
192.168.0.145 62:64:62:31:63:62
192.168.0.169 36:35:64:66:35:61
192.168.0.170 62:66:33:31:64:34
192.168.0.171 32:33:35:31:37:33
192.168.0.175 32:36:63:39:36:37
192.168.0.176 66:63:32:30:35:65
192.168.0.177 62:30:34:62:63:32
192.168.0.178 32:34:33:65:35:32
192.168.0.178 32:34:33:65:25:32
192.168.0.190 66:66:63:65:34:36
192.168.0.231 62:30:34:62:63:32
Pocket

Written by blacknon

インフラ系のSE。一時期はプログラマ。 仮想化とオープンソースに興味あり。一日中寝てたい今日このごろ。 スペインとかで働きたいなぁ…(シエスタがあるので)

Leave a Comment

メールアドレスが公開されることはありません。