Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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 exploiting the Monastery: (4)
As of 2018-06-22 04:47 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (121 votes). Check out past polls.