my $cell_name ="xyz"; while (){ if($_ =~ /STARTING_PATTERN\s+(.*)/){ $string=$_; $sub=$1; $string =~ s/$sub/$cell_name/ ; } } close FILE;