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!
In reply to split file on blank lines and match blocks by raggmopp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |