Hello.
Another with flip-flop
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.#!/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...
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
In reply to Re: split file on blank lines and match blocks
by remiah
in thread split file on blank lines and match blocks
by raggmopp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |