Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: split file on blank lines and match blocks

by kcott (Canon)
on Sep 09, 2012 at 10:26 UTC ( #992573=note: print w/ replies, xml ) Need Help??

in reply to split file on blank lines and match blocks

As the data within define host{ ... } are effectively key/value pairs, you can capture that into a Perl hash then directly access host_name, address, etc. as required.

#!/usr/bin/env perl use strict; use warnings; my $define_re = qr{ \A define \s+ host \{ \s+ ( .*? ) \s+ \} \s* \z }m +sx; my $crit_dmz_re = qr{ (?> CRIT | DMZ ) }x; my $out_format = qq{%s (%s) in groups: %s\n}; local $/ = ''; while (<DATA>) { /$define_re/; my %block = ( split /\s+/ => $1 ); if ($block{hostgroups} =~ /$crit_dmz_re/) { printf $out_format => @block{qw{host_name address hostgroups}} +; } } __DATA__ define host{ use HALF host_name denlas02 alias denlas02 address hostgroups LINUX,DEN,DMZ } define host{ use HALF host_name ppplas11 alias ppplas11 address hostgroups LINUX,PPO,ORAPRD } define host{ use ALIVE host_name ppplas12 alias ppplas12 address hostgroups LINUX,GRID,RMAN,CRIT }


$ denlas02 ( in groups: LINUX,DEN,DMZ ppplas12 ( in groups: LINUX,GRID,RMAN,CRIT

-- Ken

Comment on Re: split file on blank lines and match blocks
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2015-12-01 04:02 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (792 votes), past polls