LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-26-2004, 03:41 AM   #1
philipina
Member
 
Registered: May 2003
Posts: 77

Rep: Reputation: 15
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.
 
Old 07-26-2004, 04:07 AM   #2
Vookimedlo
Member
 
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253

Rep: Reputation: 32
Maybe:
Code:
foreach $line (@lines)
{
 $line=~ s/VALUES (/VALUES (1234,5678/
}
 
Old 07-26-2004, 04:35 AM   #3
philipina
Member
 
Registered: May 2003
Posts: 77

Original Poster
Rep: Reputation: 15
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.
 
Old 07-26-2004, 04:55 AM   #4
Vookimedlo
Member
 
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253

Rep: Reputation: 32
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.
 
Old 07-26-2004, 05:09 AM   #5
philipina
Member
 
Registered: May 2003
Posts: 77

Original Poster
Rep: Reputation: 15
Thanks a lot it works fine.

Alain.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and replace string Johng Programming 9 01-13-2010 04:50 AM
replace a substring with another string in C zeppelin Programming 21 11-09-2009 09:59 PM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 10:58 AM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM


All times are GMT -5. The time now is 02:26 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration