Re: Problem with replacing string in file



perl pra am Samstag, 25. November 2006 13:40:
hi Gurus,

I have a problem to replace strings of file thru perl script.
[...]
I have a text file some thing like this..
PROJ_FOLER=C:\Proj
PROJ_LOGS=C:\PROJ\LOGS

I have same line in config file some thing like this.
PROJ_FOLDER=D:\Proj
PROJ_LOGS=D:\PROJ\LOGS.
[...]
Here is the code i have written...

Hi perl pra

I did not test your code, and won't present a solution, because I think it is
more helpful to make you think about what you coded, and how you can simplify
coding, and then you will find the solution yourself :-)

#!/usr/bin/perl

Never forget:

use strict;
use warnings;

you have to declare all variables now, and will see warnings and hints about
possible error sources.

my $config_path="E:/MessageArchive/WorkArea/config.txt";
$file="E:/temp/FT/config/FTMessageArchive.configd";
open FH, "$config_path";

Replace the couble qoutes with single qoutes in the first two lines, no
variable is interpolated; in the third, a variable without any static text is
unnecessarily interpolated, so you can omit quoting at all.

Note the usage of FH and $LOGFILE as file handles. In newer style, use a
variable:

open my $fh, '<', $config_path or die $!;

while ($line=<FH>) {
my ($key,$val)= $line =~ /^(\w+)=(.+)$/mg ;

The test if the matching succeeded is missing. Your data may be proper
formatted or not. If not, $key and $val will be undefined, leading to
unexpected results in the following code. Never assume properly formatted
input data.

You only read one line, so the /m modifier is useless.

A data line, I assume, should only have one 'X=Y', so the /g modifier is a bit
strange. You may want to handle unexpected data lines in some way.

$repline="$key=$val";

You break $line into parts and then put it together in $repline again. I don't
see at the moment what's the sense behind it?!

open $LOGFILE, '<', $file;

*Always* check:

open $LOGFILE, '<', $file or die $!; # even better: more verbose msg

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

*Always* check. system returns 0 on success.

Then, the error messages you get, indicate that the code given to system
contains some errors. What to do? Simply print out it to see what you pass to
system. Eventually run the code directly in the shell.

You deal with user provided input here that is passed to the shell. So be
*very* cautious about what is executed, and consider malicious input data.

Also note that you are in a while loop stepping through $file, and you try to
modify $file via system. Generally it's a bad idea to alter something you're
looping through.

close $LOGFILE;

*Always* check.

last;
}
}
}

close(FH);

*Always check.

END _____________

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.
[...]
What am i doing wrong?

The most important point I think is that you should check as much as possible
and assume as less as possible :-)

Then, as a next step, you may consider a redesign. Consider (poor pseudocode):

while ...{
while ...{
system...
}
}

I don't know how many lines your files contain, but system could be executed
numerous times, every time creating a new process!



Dani (nonguru)
.



Relevant Pages

  • Re: Regarding reg. exp.
    ... I have a string and I need to parse that string to check whether it is ... in required format or not. ... I have mentioned in the Perl script that input should be in the ...
    (perl.beginners)
  • What could potentially be wrong in this script?
    ... I am writing a Perl script to check that dependency files exist that ... opening it, and then based on the type of file, saving a regex to ... foreach my $files ... The index call isn't working on this bizarre string. ...
    (comp.lang.perl.misc)
  • Re: Using js to launch Windows Save As dialog
    ... generated with a Perl script, and I want the user to be able to press a ... clearly labeled button at the bottom of the page and bring up the ... In this case, browser-sniffing may be justified, since there may be no ... UA string won't tell you for sure, so you are pretty much out of luck ...
    (comp.lang.javascript)
  • How to get "perl -V"s output programmatically?
    ... Is there a better way to get, within a Perl script, the string ... sub Config::config_re { ...
    (comp.lang.perl.misc)
  • Re: Opinions on this code please guys....
    ... to see if the string writes off the end of the buffer. ... there's no check for error (or even EOF) which is quite ... This is allso undefined behavior and stupid. ... Even the above is horrendously ineffiient and stupid. ...
    (comp.lang.cpp)