Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Problem with regex ...

by jandrew (Hermit)
on Dec 26, 2012 at 19:24 UTC ( #1010416=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: Problem with regex ...
Download Code
Re^2: Problem with regex ...
by jandrew (Hermit) on Dec 26, 2012 at 21:06 UTC

    A slightly more extensible version with a complete dispatch array implementation matched to named capture regular expressions for the columns. (Use the same data file.)

    Update: removed un-needed variable declarations, still requires perl 5.10 or higher

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1010416]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-07-14 06:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (255 votes), past polls