-2

我有这样的输出:

IPv6 Address                            Age    Link-layer Addr   State  Circuit
2001::1                                 0      00:30:88:13:28:b9 intf   1/1 vlan-id 100
2001::2                                 62     00:00:00:00:00:00 incmp  1/1 vlan-id 100
2002::1                                 0      00:30:88:13:28:b9 intf   1/1 vlan-id 101
2002::2                                 63     00:00:00:00:00:00 incmp  1/1 vlan-id 101
fe80::201:4ff:fe00:0                    14     00:00:00:00:00:00 incmp  1/1 vlan-id 100
fe80::230:88ff:fe13:28b9                0      00:30:88:13:28:b9 intf   1/1 vlan-id 100
fe80::201:5ff:fe01:0                    60     00:00:00:00:00:00 incmp  1/1 vlan-id 101
fe80::230:88ff:fe13:28b9                0      00:30:88:13:28:b9 intf   1/1 vlan-id 101

从这里我想单独存储年龄列值。如何在这里使用 split 或 grep 命令?

4

2 回答 2

1
perl -anE 'next if $. == 1; say "age: $F[1]"' file

找到最大年龄:

use List::Util qw(max);
my $max_age = max map { (split)[1] } `command`;

没有模块:

my ($max_age) = sort { $b <=> $a } map { (split)[1] } `command`;
于 2013-08-28T08:49:47.107 回答
-1

如果您的列由固定宽度分隔(例如 \t 或一定数量的空格),这将很容易。假设它们不是,并且假设你想读入你的文件,我会使用一个简单的正则表达式来提取年龄值,并且对于输入文件的每一行(这里是'input.txt')将它们推送到一个名为 的数组@age

#!/usr/bin/perl 
use strict;
use warnings;
use File::Slurp;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;

my @input = read_file('input.txt');
shift @input;

my @age; 
foreach (@input){
    my (@match) = ($_ =~ /\:\w+\s+(\d+)\s+/);
    push @age, @match;
}

print Dumper (\@age);

输出:

$VAR1 = [
          '0',
          '62',
          '0',
          '63',
          '14',
          '0',
          '60',
          '0'
        ];

更新

Data Dumper 只是提供了一种可视化数据结构中的内容的方法。但是,如果您不想使用它,则只需更改为:

#!/usr/bin/perl 
use strict;
use warnings;


my $input = 'input.txt';
open my $file, '<', $input or die "Can't open $input: $!";

my @age;
while (<$file>){
    my (@match) = ($_ =~ /\:\w+\s+(\d+)\s+/);
    push @age, @match;
}

foreach (@age){
    print "$_\n";
}

输出:

    0
    62
    0
    63
    14
    0
    60
    0

在任何一种情况下,它都不会改变代码的工作方式,或者信息存储在一个名为@age

于 2013-08-28T07:59:53.890 回答