Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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]
[LanX]: met a Canadian yesterday, he suggested building a wall and let the US pay for it
[choroba]: No, but lots of pictures of the Agent Orange victims, plus some deformed foeti
[LanX]: ugh
[choroba]: also, seeing communist propaganda I remember from my childhood... very strange experience

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2017-02-26 04:43 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (371 votes). Check out past polls.