my @flist = qw' 1st-device-CA_Eth.0/1 2nd-device-TX_Gig.1/1 '; print "@flist\n"; for my $file (@flist) { $file =~ s{\_[^_]+$}{, }; print "$file\n"; } #### 1st-device-CA_Eth.0/1 2nd-device-TX_Gig.1/1 1st-device-CA, 2nd-device-TX,