On Tue, Nov 16, 2010 at 9:38 AM, Jim Gibson <[email protected]> wrote:
> On 11/16/10 Tue  Nov 16, 2010  9:01 AM, "Vincent Li"
> <[email protected]> scribbled:
>
>> Hi List,
>>
>> I have a text test.txt file looks like below:
>>
>> stp instance 0 {
>>    interfaces 1.1 {
>>          external path cost 20000
>>          internal path cost 20000
>>       }
>>    vlans {
>>       internal
>>       vlan10
>>    }
>> }
>> profile http http_global {
>>    defaults from http
>>    max header size 38912
>>    encrypt cookies {
>>       "F-P-SES"
>>    }
>> }
>
> [snip]
>
>> how can I find a specific profile { } block and remove/replace that
>> part from the file, for example, I want to remove below block or
>> replace it with emtpy blank line
>>
>> profile tcp tcp {
>>    reset on timeout enable
>>    time wait recycle enable
>>    delayed acks enable
>>    proxy mss disable
>> }
>>
>> and the { } could have nested { } block too.
>>
>
> 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;
$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?

--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to