Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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+)/; ... }


Comment on Re: split file on blank lines and match blocks
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992553]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-10-20 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (75 votes), past polls