Adding number before file extension

Hi ,

I have a file which has a line starts with $segment_name and has the below data

source data

$Segment_Name = 123.ABC.123.01.txt
$Segment_Name = 123.ABC.ABC.txt
$Segment_Name = 123.ABC.12A3.txt

My target data should be

$Segment_Name = 123.ABC.123.01.txt
$Segment_Name = 123.ABC.ABC.01.txt
$Segment_Name = 123.ABC.12A3.01.txt

I found the below code from this forum, it is adding the .01 extension to all the lines as below

sed '/^$Segment_Name/ s/\([^\d]\).txt/\01\.01\.txt/' input
$Segment_Name = 123.ABC.123.01.01.txt
 $Segment_Name = 123.ABC.ABC.01.txt
 $Segment_Name = 123.ABC.12A3.01.txt

I don't want to add 01 before the file extension if the 01 is already present(line no 1 in the above example).please help me to tweak the code

Regards,Shruthi

 nawk -F\. '{if($(NF-1)!~/01/){gsub(/txt/,"01.txt");print} else {print $0}}'  filename

or

sed '/01.txt/!s/txt/01.txt/g' filename

Thanks
Sha

Try this

 
perl -lne '$_=~ s/\.txt/\.01\.txt/ if /(\w+)\.\w+$/ && $1 ne "01";print'