LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl : Troubles to replace a string. (https://www.linuxquestions.org/questions/programming-9/perl-troubles-to-replace-a-string-209712/)

philipina 07-26-2004 03:41 AM

Perl : Troubles to replace a string.
 
Hello,

I'm a newbie in Perl and I created a file with a DB dump (by using mysqldump). I would like to open this dump file "dump.txt" and change it to add some parameters with a perl script.

Here is my code:

$file='dump.sql';
#open the file
open(FILE, $file);
@lines = <FILE>;
close(FILE);

@lines=~ s/VALUES (/VALUES (1234,5678/;
print @lines;

But I receive the following error : "Can't modify array dereference i n substitution..."

I assume that this happen because I'm using an array but the question is "How can I do that in another way?".

Also, I would like to know which one is the fastest to do this kind of modification in a file with more than 2000 lines "bash or perl"?

Thank in advance.

Alain.

Vookimedlo 07-26-2004 04:07 AM

Maybe:
Code:

foreach $line (@lines)
{
 $line=~ s/VALUES (/VALUES (1234,5678/
}


philipina 07-26-2004 04:35 AM

Thank you,

I tried it but I still get an error.

I tried 6 syntaxes:

foreach $line (@lines)
{
$line=~ s/VALUES (/VALUES (1234,5678/
}

-----------------------------------

foreach $line (@lines)
{
$line=~ s/'VALUES ('/'VALUES (1234,5678/'
}

--------------------------------------
foreach $line (@lines)
{
$line=~ s/"VALUES ("/"VALUES (1234,5678/"
}


and same syntax also ending by ;

Thanks in advance.

Regards.

Alain.

Vookimedlo 07-26-2004 04:55 AM

Sorry, my mistake:

you have to protect ( ) chars, because these are reserved for special meaning in pattern

so:
Code:

foreach $line (@lines)
{
 $line=~ s/VALUES \(/VALUES \(1234,5678/
}

BTW:
there is no need to have " or ' in regular pattern, its another pattern that will be searched.

; after the last commnd in the block is only optional.

philipina 07-26-2004 05:09 AM

Thanks a lot it works fine.

Alain.


All times are GMT -5. The time now is 03:26 AM.