Results 1 to 4 of 4
Hi everyone
I'm trying to do a "search and replace" on a cgi-file.
I'm looking to replace d1 with the variable $a . Here is my script.
Code:
my @contents;
...
- 05-06-2009 #1Linux User
- Join Date
- Jul 2007
- Location
- Greece
- Posts
- 277
search and replace doesn't work as expected (Perl)
Hi everyone
I'm trying to do a "search and replace" on a cgi-file.
I'm looking to replace d1 with the variable $a. Here is my script.
The above will replace the first occurrence of d and 1 but not d1. It shouldn't do that, isn't it?Code:my @contents; my ($fileread, $filewrite, $line); my $infile = '/var/www/cgi-bin/test'; open ($fileread, $infile) || die ("Can't open: $infile $!"); my (@contents) = (<$fileread>); close ($fileread); open ($filewrite, '>', $infile) || die ("Can't open: $infile $!"); foreach $line (@contents) { $line=~ s/d1/$a/; print $filewrite $line; } close ($filewrite);
Is there anything I'm doing wrong here, Any suggestions please?
- 05-06-2009 #2Linux User
- Join Date
- Aug 2006
- Posts
- 458
should be \$a
- 05-06-2009 #3Linux User
- Join Date
- Jul 2007
- Location
- Greece
- Posts
- 277
Thanks for the reply ghostdog but
replaced d1 with $a instead of what $a is equal to.Code:\$a
- 05-06-2009 #4Linux User
- Join Date
- Jul 2007
- Location
- Greece
- Posts
- 277
I just realised that the script above searches and replaces the first occurrence of d1 in each line. Do you know how I can prevent this from happening?


Reply With Quote