#!/usr/bin/perl use strict; use warnings; my ($buff,$hostname,$address); my $criteria="DMZ|CRIT"; while(){ #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... #### egrep -A 2 -B 5 "hostgroups.*(CRIT|DMZ)" your_config_file