Re: Problem with replacing string in file



Mumia W. wrote:

On 11/25/2006 06:40 AM, perl pra wrote:

Here is the code i have written...

[snip]

open $LOGFILE, '<', $file;
while ($line1 = <$LOGFILE> ) {
if ($line1 =~ m/$key/) {
system("perl -i.bak -p -e 's/$line1/$repline/g' $file");
close $LOGFILE;
last;
}
}

[snip]

if i run the script I am getting the following errror..


Can't find string terminator "'" anywhere before EOF at -e line 1.
Can't find string terminator "'" anywhere before EOF at -e line 1.
Can't find string terminator "'" anywhere before EOF at -e line 1.
Can't find string terminator "'" anywhere before EOF at -e line 1.
Can't find string terminator "'" anywhere before EOF at -e line 1.
Can't find string terminator "'" anywhere before EOF at -e line 1.
Can't find string terminator "'" anywhere before EOF at -e line 1.

What am i doing wrong?


My eyes are too old to see a stray ' somewhere in a program. My guess is
that it has something to do with the call to system (which shouldn't be
called anyway).


It's for two reasons. Firstly $line1 isn't chomped, so there is a newline before
the second slash in the substitution; and secondly Windows doesn't recognise
single quotes as being special on a command line, so Perl is being passed a
parameter list like:

-i.bak
-p
-e
's/PROJ_FOLDER=D:\Proj
/PROJ_FOLDER=D:\Proj/g'
E:/temp/FT/config/FTMessageArchive.configd

and clearly the one-liner has no string terminator!

Rob

.



Relevant Pages