Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
We don't bite newbies here... much
 
PerlMonks  

Re: split file on blank lines and match blocks

by kcott (Abbot)
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 146.xxx.xxx.xxx hostgroups LINUX,DEN,DMZ } define host{ use HALF host_name ppplas11 alias ppplas11 address 10.xxx.xxx.xxx hostgroups LINUX,PPO,ORAPRD } define host{ use ALIVE host_name ppplas12 alias ppplas12 address 10.50.33.26 hostgroups LINUX,GRID,RMAN,CRIT }

Output:

$ pm_hosts_parse.pl denlas02 (146.xxx.xxx.xxx) in groups: LINUX,DEN,DMZ ppplas12 (10.50.33.26) in groups: LINUX,GRID,RMAN,CRIT

-- Ken


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2014-04-21 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (493 votes), past polls