# variable has a return on the end, possibly $variable= "file_name_text.txt\n"; chomp $variable; # removes the return but won't remove non-whitespace. $variable =~ s/\.txt$//; # now variable has no ".txt" on the end.