in reply to Problem with regex ...
With the risk of good money after bad and acknowledging that this could have the (your prefered afterlife paradigm here) golfed out of it. I submit the following column split using named captures for each column.
#!perl my $table_spliter = qr/^Mod\s*((Ports)|(Sub))/; my $table_type; my $pre_string; my %columns = ( card_type => [ qw( SLOT PORTAS DESC Model Sw ) ], sub_module => [ qw( Slot Desc Model Sw Hw Status ) ], ); my %splitter = ( card_type => qr/ (?<$columns{card_type}->[0]>.{4}) (?<$columns{card_type}->[1]>.{6}) (?<$columns{card_type}->[2]>.{39}) (?<$columns{card_type}->[3]>.{19}) (?<$columns{card_type}->[4]>.{11}) /sxm, sub_module => qr/ (?<$columns{sub_module}->[0]>.{5}) (?<$columns{sub_module}->[1]>.{28}) (?<$columns{sub_module}->[2]>.{19}) (?<$columns{sub_module}->[3]>.{12}) (?<$columns{sub_module}->[4]>.{8}) (?<$columns{sub_module}->[5]>.{2,7}) /sxm, ); while(<DATA>){ my $line = $_; chomp $line; my @types = $line =~ $table_spliter; if( $types[1] ){ $table_type = 'card_type'; $pre_string = ''; }elsif( $types[2] ){ $table_type = 'sub_module'; $pre_string = '<> '; } $line =~ $splitter{$table_type}; my %line_hash = %+; if( exists( $line_hash{Sw} ) and $line_hash{Sw} =~ /^\w{3}\d/ ){ for my $item ( @{$columns{$table_type}} ){ my $value = $line_hash{$item}; $value =~ s/^\s*(.*?)\s*$/$1/; print $pre_string . $item . ": " . $value . "\n"; } }else{ print "Sw hash is: $line_hash{Sw}\n" if exists $line_hash{Sw}; print "Cant print: $line\n"; } } close(DATA); __DATA__ Mod Ports Card Type Model Se +rial No. --- ----- -------------------------------------- ------------------ -- +--------- 1 24 CEF720 24 port 1000mb SFP WS-X6724-SFP SA +L1434RA09 3 20 7600 ES+T 76-ES+T-20G JA +E14530455 5 2 Route Switch Processor 720 (Active) RSP720-3CXL-GE JA +E14330N9B 6 2 Route Switch Processor 720 (Hot) RSP720-3CXL-GE JA +E14330NA6 7 4 CEF720 4 port 10-Gigabit Ethernet WS-X6704-10GE SA +L1433QVJQ 8 4 CEF720 4 port 10-Gigabit Ethernet WS-X6704-10GE SA +L1433QVJW Mod Sub-Module Model Serial Hw + Status ---- --------------------------- ------------------ ----------- ------ +- ------- 1 Distributed Forwarding Card WS-F6700-DFC3CXL SAL1434RLPY 1.6 + Ok 3 7600 ES+ DFC XL 7600-ES+3CXL JAE14520N29 1.2 + Ok 3 7600 ES+T 20x1GE SFP 76-ES+T-20GQ JAE145301XM 1.1 + Ok 5 Policy Feature Card 3 7600-PFC3CXL JAE14330E6J 1.1 + Ok 5 C7600 MSFC4 Daughterboard 7600-MSFC4 JAE14320QBE 1.6 + Ok 6 Policy Feature Card 3 7600-PFC3CXL JAE14330EAO 1.1 + Ok 6 C7600 MSFC4 Daughterboard 7600-MSFC4 JAE14320QA8 1.6 + Ok 7 Distributed Forwarding Card WS-F6700-DFC3CXL SAL1433QHBR 1.6 + Ok 8 Distributed Forwarding Card WS-F6700-DFC3CXL SAL1433QXF9 1.6 + Ok
Update: fixed the position split. Requires perl 5.10 or higher.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Problem with regex ...
by jandrew (Chaplain) on Dec 26, 2012 at 21:06 UTC |
In Section
Seekers of Perl Wisdom