Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992579]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2017-12-14 01:48 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (383 votes). Check out past polls.