- 论坛徽章:
- 307
|
本帖最后由 sunzhiguolu 于 2017-01-16 19:19 编辑
- #!/usr/bin/perl
- local $/ = '';
- my @aFieldList = qw /Port NoField PortEnable MediaType Duplex Speed Class Link RecentUpTime/;
- my %hFieldList = map {$_ => 1} @aFieldList;
- my %hDefaults = ('Port' => 1, 'PortEnable' => 'enable'); # 默认值, 可自行指定
- my %hTrace = ();
- my ($flag, %hData) = (1, ());
- while (<>){
- my ($id, @aLines) = split (/\n/, $_);
- $id = $1 if ($id =~ /(\d+)/);
- foreach (splice (@aLines, -2)){
- my @aT = split;
- push (@{$hData{$id}{$aT[0]}}, "@aT[2 .. $#aT]") if ($hFieldList{$aT[0]});
- }
- foreach (@aLines){
- my @aFields = split;
- next if ($aFields[0] eq 'Params' or $aFields[0] eq 'Status');
- while (/(\S+)\s*:\s*(\S+)/g){
- next if (!$hFieldList{$1});
- push (@{$hData{$id}{$1}}, $2);
- $hTrace{$1}++ if ($flag);
- }
- }
- $flag = 0 if ($flag);
- }
- push (@aFieldList, $_) for grep {$hTrace{$_} > 1} keys %hTrace;
- @aFieldList = sort {$a cmp $b} @aFieldList;
- print join (',', @aFieldList), "\n";
- my @aOut = ();
- foreach my $id (sort {$a <=> $b} keys %hData){
- my %hCollect = ();
- my @aT = ();
- foreach my $k (sort {$a cmp $b} keys %{$hData{$id}}){
- push (@{$hCollect{$k}}, @{$hData{$id}{$k}});
- }
- foreach my $k (sort {$a cmp $b} @aFieldList){
- push (@{$hCollect{$k}}, exists ($hDefaults{$k}) ? $hDefaults{$k} : 'NULL') if (!exists ($hCollect{$k}));
- }
- foreach my $k (sort {$a cmp $b} keys %hCollect){
- push (@aT, @{$hCollect{$k}});
- }
- push (@aOut, join (',', @aT) . "\n");
- }
- print @aOut;
复制代码 perl abc.pl urFile
测试记录行数:850000
real 3.99 user 3.90 sys 0.09
real 3.99 user 3.93 sys 0.06
real 4.00 user 3.90 sys 0.09
real 4.00 user 3.90 sys 0.09
real 4.00 user 3.91 sys 0.07
real 4.00 user 3.91 sys 0.09
real 4.00 user 3.96 sys 0.04
real 4.00 user 3.97 sys 0.03
real 4.02 user 3.90 sys 0.10
real 4.02 user 3.96 sys 0.04
real 4.02 user 3.96 sys 0.06
real 4.04 user 3.86 sys 0.12
|
|