Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

Hello.

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (13)
As of 2014-08-21 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (136 votes), past polls