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

Re: split file on blank lines and match blocks

by 2teez (Priest)
on Sep 09, 2012 at 03:41 UTC ( #992556=note: print w/ replies, xml ) Need Help??

in reply to split file on blank lines and match blocks

...But I'm looking for the host_name and address in addition to the line...

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my %host_add; my ( $host, $name ); while (<DATA>) { s/^\s+|\s+$//; if (/^host_name/) { ( $host, $name ) = split /\s+/, $_; if ( !exists $host_add{$name} ) { undef $host_add{$name}; } else { next } } elsif ( /^address/ or /^hostgroups/ ) { my ( $add, $address ) = split /\s+/, $_; push @{ $host_add{$name} }, $address; } } print Dumper \%host_add; __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 }
$VAR1 = { 'denlas02' => [ '', 'LINUX,DEN,DMZ' ], 'ppplas11' => [ '', 'LINUX,PPO,ORAPRD' ], 'ppplas12' => [ '', 'LINUX,GRID,RMAN,CRIT' ] };
With the above as output, you can get the host, hostgroup, and address..
Add the following to the script like so:
for my $val ( keys %host_add ) { for ( @{ $host_add{$val} } ) { print $val, q{ }, join " ", @{ $host_add{$val} }, $/ if $_ =~ m{CRIT|DMZ}; } }
Then, your output will be:
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

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://992556]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2015-11-29 10:08 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 (750 votes), past polls