search and replace

Hi everyone

I've written the following and the code runs ok but not as expected. I want to replace d1 with variable $IP5. What happens is that d1 is being replaced for each line on the html file below although I didn't include the g option in $line=~ s/d1/$IP5/;
The file looks like this:

1) <TR ALIGN="CENTER">
2) <TD>P1</TD> <TD>d1</TD> <TD>d2</TD> <TD>d3</TD> <TD>d4</TD>
3) <TD>d5</TD> <TD>d6</TD>
4) </TR>
5) <TR ALIGN="CENTER">
6) <TD>P2</TD> <TD>d7</TD> <TD>d8</TD> <TD>d9</TD> <TD>d10</TD> <TD>d11</TD> <TD>d12</TD>
8) </TR>

When I run the code not only d1 in line 2 is being replaced but also d1 in line 6
How can I prevent this from happening? Any suggestions, please?

I'm thankful for your help. Please see below the script.

my $filein= '/var/www/cgi-bin/info2';
my ($fileread, $filewrite, $line, @contents);

open ($fileread, $filein) || die ("Can't open: $filein $!");

          my (@contents) = (<$fileread>);  
          close ($fileread);

open ($filewrite, '>', $filein) || die ("Can't open: $filein $!");
     
          foreach $line (@contents)
          { 
             $line=~ s/d1/$IP5/;
             print $filewrite $line;
                                              }
close ($filewrite);

Try to match only d1

$line=~ 's/>d1</>$IP5</'

cheers,
Devaraj Takhellambam

Excellent! Thank you Devaraj, thank you, thank you.