undef $/; open(IN, "< 1.txt") ; $str = ; close(IN); $str =~ s#\n(.+?)\n#\n\$exec \= \$ENV\{EXEC\}\n$1\n#gsi; open(OUT, "> 2.txt") ; print OUT $str; input: ********* hai this is the test input sample output: ********** hai $exec = $ENV{EXEC} this is $exec = $ENV{EXEC} the test $exec = $ENV{EXEC} input sample