FAQ
hi Gurus,

I have a problem to replace strings of file thru perl script.


Here is the problem in detail...


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.


Now i read variables from text file split them like this

my ($key,$val)= $line =~ /^(\w+)=(.+)$/mg ;

and place both in a varaible $rep_line="$key=$val";

and search in the config file with $key (which has PROJ_FOLDER) and get to
the result to $line.

Now i will replace $line with $rep_line using

perl -i -p -e 's/$line/$rep_line/g' $file;

Here is the code i have written...

BEGIN________

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

while ($line=<FH>)
{


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

}
}
close(FH);
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.
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?

Please help me on this..


Thanks in Advance

PP..

Search Discussions

  • D. Bolliger at Nov 25, 2006 at 1:58 pm

    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)
  • Mumia W. at Nov 25, 2006 at 3:28 pm

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

    I have a problem to replace strings of file thru perl script.


    Here is the problem in detail...


    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.


    Now i read variables from text file split them like this

    my ($key,$val)= $line =~ /^(\w+)=(.+)$/mg ;

    and place both in a varaible $rep_line="$key=$val";

    and search in the config file with $key (which has PROJ_FOLDER) and get to
    the result to $line.

    Now i will replace $line with $rep_line using

    perl -i -p -e 's/$line/$rep_line/g' $file;

    Here is the code i have written...

    BEGIN________

    #!/usr/bin/perl
    Missing:
    use strict;
    use warnings;

    Modify your program to work with them.
    my $config_path="E:/MessageArchive/WorkArea/config.txt";
    $file="E:/temp/FT/config/FTMessageArchive.configd";
    open FH, "$config_path";

    while ($line=<FH>)
    {


    my ($key,$val)= $line =~ /^(\w+)=(.+)$/mg ;
    $repline="$key=$val";
    open $LOGFILE, '<', $file;
    while ($line1 = <$LOGFILE> )
    {
    if ($line1 =~ m/$key/){ system("perl -i.bak -p -e
    's/$line1/$repline/g' $file");close $LOGFILE;last;}
    Inline editing, on my platform, results in the original file being
    renamed and eventually deleted. Why are you creating a separate perl
    process to modify a file you already have open?

    }
    }
    close(FH);
    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.
    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).
    Please help me on this..


    Thanks in Advance

    PP..
    HTH
  • Rob Dixon at Nov 25, 2006 at 4:37 pm

    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
  • Perl pra at Nov 29, 2006 at 1:36 pm
    thanks rob and Samstag..
    have done it

    thanks a lot.

    On 11/25/06, Rob Dixon wrote:

    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


    --
    To unsubscribe, e-mail: [email protected]
    For additional commands, e-mail: [email protected]
    <http://learn.perl.org/> <http://learn.perl.org/first-response>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedNov 25, '06 at 12:40p
activeNov 29, '06 at 1:36p
posts5
users4
websiteperl.org

People

Translate

site design / logo © 2023 Grokbase