1

从下面的输出中,我只想创建一个可以存储所有 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
4

2 回答 2

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并最终打印出来。

于 2013-04-01T05:41:23.713 回答
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
于 2013-04-01T07:36:27.747 回答