On 11/16/10 Tue Nov 16, 2010 1:07 PM, "Vincent Li" <[email protected]> scribbled:
> On Tue, Nov 16, 2010 at 9:38 AM, Jim Gibson <[email protected]> wrote: >> >> You need a parser to do this right. You might have some luck by reading the >> entire file into a scalar variable and using the Text::Balanced module >> together with some regular expressions to extract and parse the various >> blocks within the file. >> >> A more rigorous approach would be to create a real parser with the >> Parse::RecDescent module and create a grammar for your file. However, that >> module has a steep learning curve and can be slow. > > thanks for the reply, maybe I just make everyone think too deep for > this problem, if that is true, sorry about that. let me rephrase what > I want to achieve. the file look like this: > > vlan vlan10 { > tag 1330 > interfaces tagged 1.1 > } > route domain 10 { > parent id 0 > description "RD10" > vlans vlan10 > } > profile smtp smtp { > defaults from none > security enabled enable > } > profile oneconnect oneconnect_global { > defaults from oneconnect > } > profile tcp tcp { > reset on timeout enable > time wait recycle enable > } > self 10.99.91.1 { > netmask 255.255.255.0 > vlan vlan11 > allow default > } > failover { > standby link down time 0 > } > > My aim is to remove specific profile.*{} block from that file, I have > an half working script I wrote here: > > #!/usr/bin/perl > use strict; > use warnings; > > my $holdTerminator = $/; > undef $/; > open(my $fh, '<', "/tmp/test1.scf") or die $!; > my $text = <$fh>; > while(<DATA>) { > chop; Try chomp here instead of chop. > $text =~ s/$_\s{.*?}//gs; > } > #$text =~ s/profile tcp tcp\s{.*?}//gs; > print $text; > $/ = $holdTerminator; > > __DATA__ > profile auth ssl_ocsp > profile stats stats > profile stream stream > profile auth tacacs > profile tcp tcp > profile tcp tcp-cell-optimized > profile tcp tcp-lan-optimized > profile tcp tcp-wan-optimized > profile udp udp > > > my intention is to find any profile list in __DATA__ and remove the > whole block from that file, now the problem is in the while loop, it > appear that the regex and substituation is not working, but if comment > out the while loop block and hard code the profile pattern in the > subsituation regex, it works, what I am missing? What platform are you using? I did have the same problem until I made sure there were only linefeeds in the file and I used chomp instead of chop. The search pattern I was reading from <DATA> contained a carriage return "\r" on the end. Run your output through a hex/octal dump program, e.g. on unix: perl program.pl | od -c to see the characters, and print the search patterns you read from <DATA> in your program: while(my $pat = <DATA>) { chomp($pat); print "Apply <$pat>\n"; $text =~ s/$pat\s{.*?}//gs; } -- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] http://learn.perl.org/
