Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: split file on blank lines and match blocks

by mbethke (Hermit)
on Sep 09, 2012 at 03:20 UTC ( #992553=note: print w/replies, xml ) Need Help??

in reply to split file on blank lines and match blocks

Setting the record separator would help: $/ = "\n\n";. After that, a while(<>) { ... } will loop over individual records which you can then dissect using regexps.

However, that would be a hack as it doesn't follow the actual syntax of the Nagios file but exploits the fact that someone nice has put exactly one blank line between all records. Should you or your admin forget that one day, Nagios will still work but your script won't. I'm not quite sure if there are any subtleties such as escape characters to Nagios' config file syntax but I think it's pretty simple. I'd probably slurp the whole file into a string and then parse blocks with a regexp, using further matching on the whole block to find the relevant pieces of data:

my $file = do { local $/; <$filehandle> }; while($file =~ / define \s+ host \s* { (.*?) } \s* /gsx) { my ($hostname) = $1 =~ /host_name\s+(\S+)/; ... }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992553]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2017-04-27 06:21 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (501 votes). Check out past polls.