从下面的输出中,我只想创建一个可以存储所有IP地址的数组。
IP示例:57.35.47.54
输出:
Codes: '!' - success, 'Q' - request not sent, '.' - timeout,
'L' - labeled output interface, 'B' - unlabeled output interface,
'D' - DS Map mismatch, 'F' - no FEC mapping, 'f' - FEC mismatch,
'M' - malformed request, 'm' - unsupported tlvs, 'N' - no label entry,
'P' - no rx intf label prot, 'p' - premature termination of LSP,
'R' - transit router, 'I' - unknown upstream index,
'X' - unknown return code, 'x' - return code 0
Type escape sequence to abort.
0 57.35.47.54 MRU 4470 [Labels: implicit-null/1852 Exp: 0/0]
I 1 57.35.47.53 MRU 4470 [Labels: 16176 Exp: 0] 328 ms
L 2 57.35.57.1 MRU 4470 [Labels: explicit-null/2795 Exp: 0/0] 140 ms
D 3 57.35.7.105 MRU 4470 [Labels: 16228 Exp: 0] 364 ms
I 4 57.35.4.78 MRU 4470 [Labels: implicit-null/16304 Exp: 0/0] 196 ms
L 5 57.35.7.66 MRU 4470 [Labels: 16613 Exp: 0] 216 ms
L 6 57.35.4.38 MRU 4470 [Labels: implicit-null/implicit-null Exp: 0/0] 216 ms
! 7 57.35.4.122 288 ms
答案 0 :(得分:2)
也许以下内容会有所帮助:
use strict;
use warnings;
use Regexp::Common qw/net/;
my @IPs;
while (<DATA>) {
push @IPs, $1 while /($RE{net}{IPv4})/g;
}
print "$_\n" for @IPs;
__DATA__
Codes: '!' - success, 'Q' - request not sent, '.' - timeout,
'L' - labeled output interface, 'B' - unlabeled output interface,
'D' - DS Map mismatch, 'F' - no FEC mapping, 'f' - FEC mismatch,
'M' - malformed request, 'm' - unsupported tlvs, 'N' - no label entry,
'P' - no rx intf label prot, 'p' - premature termination of LSP,
'R' - transit router, 'I' - unknown upstream index,
'X' - unknown return code, 'x' - return code 0
Type escape sequence to abort.
0 57.35.47.54 MRU 4470 [Labels: implicit-null/1852 Exp: 0/0]
I 1 57.35.47.53 MRU 4470 [Labels: 16176 Exp: 0] 328 ms
L 2 57.35.57.1 MRU 4470 [Labels: explicit-null/2795 Exp: 0/0] 140 ms
D 3 57.35.7.105 MRU 4470 [Labels: 16228 Exp: 0] 364 ms
I 4 57.35.4.78 MRU 4470 [Labels: implicit-null/16304 Exp: 0/0] 196 ms
L 5 57.35.7.66 MRU 4470 [Labels: 16613 Exp: 0] 216 ms
L 6 57.35.4.38 MRU 4470 [Labels: implicit-null/implicit-null Exp: 0/0] 216 ms
! 7 57.35.4.122 288 ms
输出:
57.35.47.54
57.35.47.53
57.35.57.1
57.35.7.105
57.35.4.78
57.35.7.66
57.35.4.38
57.35.4.122
Regexp::Common包含一个与IPv4地址匹配的正则表达式,并在上面用于在每一行上捕获它们。虽然您的数据每行只包含一个IP地址,但每条线路也会捕获多个IP。将捕获的IP push
编辑到@IPs
,然后最终打印。
答案 1 :(得分:1)
如果您无法安装缺少的regexp:common模块,您可以手动解析它
use strict;
use warnings;
while (<DATA>) {
while(/([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/g) {
print "$1.$2.$3.$4\n" if ($1 < 256 && $2 < 256 && $3 < 256 && $4 <256);
}
}
__DATA__
Codes: '!' - success, 'Q' - request not sent, '.' - timeout,
'L' - labeled output interface, 'B' - unlabeled output interface,
'D' - DS Map mismatch, 'F' - no FEC mapping, 'f' - FEC mismatch,
'M' - malformed request, 'm' - unsupported tlvs, 'N' - no label entry,
'P' - no rx intf label prot, 'p' - premature termination of LSP,
'R' - transit router, 'I' - unknown upstream index,
'X' - unknown return code, 'x' - return code 0
Type escape sequence to abort.
0 57.35.47.54 MRU 4470 [Labels: implicit-null/1852 Exp: 0/0]
I 1 57.35.47.53 MRU 4470 [Labels: 16176 Exp: 0] 328 ms
L 2 57.35.57.1 MRU 4470 [Labels: explicit-null/2795 Exp: 0/0] 140 ms
D 3 57.35.7.105 MRU 4470 [Labels: 16228 Exp: 0] 364 ms
I 4 57.35.4.78 MRU 4470 [Labels: implicit-null/16304 Exp: 0/0] 196 ms
L 5 57.35.7.66 MRU 4470 [Labels: 16613 Exp: 0] 216 ms
L 6 57.35.4.38 MRU 4470 [Labels: implicit-null/implicit-null Exp: 0/0] 216 ms
! 7 57.35.4.122 288 ms