use strict ; use warnings ; my $file = "test" ; open (my $fhi, "<", $file . ".dat" ) or die "Cannot open $file.dat\n" ; open (my $fho, ">", $file . "_out.dat" ) or die "Cannot open $file" . "_out.dat\n" ; my $context = "" ; while ( my $line = <$fhi> ) { chomp $line ; if ( $line =~ /ObjectType1/ ) { $context = "ObjectType1" ; } if ( $line =~ /$\}/ ) { $context = "" ; } if ( $context eq "ObjectType1" ) { if ( $line =~ /Param1/ ) { print $fho "Param1 = 0\n" ; } elsif ( $line =~ /Param2/ ) { print $fho "Param2 = SomeOtherText\n" ; } else { print $fho $line . "\n" ; } } else { print $fho $line . "\n" ; } }