raggmopp has asked for the wisdom of the Perl Monks concerning the following question:
Hi all. Working with perl 5.8.8 installed on OEL 5.
I am trying to parse a Nagios formatted hosts.cfg file. Each host is separated from the other with a blank line so there is a block related to each host. Each host is also defined with the hostgroups it is a member of.
I am having a hard time trying to figure out what I am missing and the worse part is I'm sober.
A snippet of the 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 }
I have been trying and trying to identify hosts that are members of the CRIT hostgroup, or of the DMZ hostgroup, or the ... The best I have come up with is to print the line that matches. But I'm looking for the host_name and address in addition to the line.
One of my latest attempts
#!/usr/local/bin/perl use strict; use warnings; my $file = 'hosts.cfg'; open (MYHOSTS, $file); while (<MYHOSTS>) { my @hosts = split(/\n\n/); foreach my $host (@hosts) { print if ('$host' =~ m/(GRID)/); print if grep(/RMAN/, $host); } } close (MYHOSTS);
I've tried with grep and regex, no joy. What is it I'm missing?
Many thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: split file on blank lines and match blocks
by BrowserUk (Patriarch) on Sep 09, 2012 at 03:29 UTC | |
|
Re: split file on blank lines and match blocks
by 2teez (Vicar) on Sep 09, 2012 at 03:41 UTC | |
|
Re: split file on blank lines and match blocks
by mbethke (Hermit) on Sep 09, 2012 at 03:20 UTC | |
|
Re: split file on blank lines and match blocks
by kcott (Archbishop) on Sep 09, 2012 at 10:26 UTC | |
|
Re: split file on blank lines and match blocks
by remiah (Hermit) on Sep 09, 2012 at 11:37 UTC |