output#!/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 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 }
With the above as output, you can get the host, hostgroup, and address..$VAR1 = { 'denlas02' => [ '146.xxx.xxx.xxx', 'LINUX,DEN,DMZ' ], 'ppplas11' => [ '10.xxx.xxx.xxx', 'LINUX,PPO,ORAPRD' ], 'ppplas12' => [ '10.50.33.26', 'LINUX,GRID,RMAN,CRIT' ] };
Then, your output will be:for my $val ( keys %host_add ) { for ( @{ $host_add{$val} } ) { print $val, q{ }, join " ", @{ $host_add{$val} }, $/ if $_ =~ m{CRIT|DMZ}; } }
denlas02 146.xxx.xxx.xxx LINUX,DEN,DMZ ppplas12 10.50.33.26 LINUX,GRID,RMAN,CRIT
In reply to Re: split file on blank lines and match blocks
by 2teez
in thread split file on blank lines and match blocks
by raggmopp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |