我有这样的输出:
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命令?
答案 0 :(得分: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`;
答案 1 :(得分:-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
的数组中的事实