Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: split file on blank lines and match blocks

by remiah (Hermit)
on Sep 09, 2012 at 11:37 UTC ( #992579=note: print w/ replies, xml ) Need Help??

in reply to split file on blank lines and match blocks


Another with flip-flop

#!/usr/bin/perl use strict; use warnings; my ($buff,$hostname,$address); my $criteria="DMZ|CRIT"; while(<DATA>){ #flip-flop. while /begin/ to /end/ condition, you enter the if + block #and line number is set to $num. if(my $num = /define\s+host\{/ .. /\s*\}/ ){ $buff ='' if($num==1); #for regex match keep text to $ +buff $buff .=$_; if( /\s*host_name\s+(.*)/ ){ #get hostname $hostname=$1; } if( /\s*address\s+(.*?)/ ){ #get address $address= $1; } #E0 shows end of Block. if $buff has $criteria, print if ($num =~ /E0/ && $buff =~ /$criteria/s ){ print "$address $hostname is in $criteria \n"; } } } __DATA__ same data with yours...
For flip-flop, please see this thread, which is very similar case with yours, and Grandfather's meditation for flip-flop and Range Operators at PerlOp.

One another approach with grep. But this may not work well, I am sure.

egrep -A 2 -B 5 "hostgroups.*(CRIT|DMZ)" your_config_file

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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2016-05-31 01:07 GMT
Find Nodes?
    Voting Booth?