Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: split file on blank lines and match blocks

by kcott (Chancellor)
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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992573]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2018-02-25 14:26 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (312 votes). Check out past polls.