Before I start off, I’d like to let you know that I’m no Perl expert. I’m just starting out because of some specific tasks assigned to me.
The requirement of this task is to extract the extension of the file (.dat) and replace it with .trg. The problem is we are zipping the .dat file to make $filename.dat.gz and when we extract the extension and replace it we get $filename.dat.trg while want we would ideally want is $filename.trg.
As for the code (mind you, this seems to be a very old ‘legacy’ code and I don’t want to tinker with it too much as it was/is being maintained by another person), this is how it is put down
#prepare the trigger file
#get the extension
my @contains_extension = split (/\./ , $filename);
my $ext = $contains_extension[-1];
#replace with a ".trg" extension
my $remote_trgfile = $filename;
$remote_trgfile =~ s/$ext$/trg/;
my $trgfile = $out;
$trgfile =~ s/$ext$/trg/;
Remember $filename in the above code is suffixed with .dat.gz i.e., the filename is $filename.dat.gz
I would appreciate if someone could help me out with an easier way to extract both the extensions (.dat and .gz) and replacing it with .trg
So you want to change the ‘extension’ of a filename, including a optional
.gz? Try: