#!/usr/bin/perl # https://perlmonks.org/?node_id=1227916 use strict; use warnings; ##################### configuration section my $section = 'ObjectType1'; my %changes = ( Param1 => 0, Param2 => 'SomeOtherText', Param3 => 'Foobar'); ##################### end configuration section my $allkeys = join '|', keys %changes; my $pattern = qr/\b($allkeys)\b/; local $/ = "\n}\n"; while( ) { if( /\b$section\b/ ) { my @context; print $& while @context && $context[-1] eq $section && /\G(\h*$pattern = ).*\n/gc ? "$1$changes{$2}\n" =~ /.*/s : @context && /\G\h*\}\n/gc ? pop @context : /\G\h*([\w ]+)\n\h*\{\n/gc ? push @context, $1 : /\G.*\n/gc; } else { print; } } __DATA__ ObjectType1 { Param1 = 8 NestedObject { Param1 = 3 Param2 = SomeText } Param2 = SomeText } ObjectType2 { Foo { Param1 = StaySame ObjectType1 { Param3 = ReplaceThis } } } ObjectType1 { ... } #### ObjectType1 { Param1 = 0 NestedObject { Param1 = 3 Param2 = SomeText } Param2 = SomeOtherText } ObjectType2 { Foo { Param1 = StaySame ObjectType1 { Param3 = Foobar } } } ObjectType1 { ... }