LinuxQuestions.org
Visit Jeremy's Blog.
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
 
Search this Thread
Old 02-14-2011, 06:51 PM   #1
koundinya749
LQ Newbie
 
Registered: Feb 2011
Posts: 3

Rep: Reputation: 0
Find and replace a string in a file using perl command from bash script


Hi,
I wanted to find and replace a string from a perl file. I have written a script in bash which runs the following command.

perl -pi -e "s/$findstring/$replacestring/" testfile

where as $findstring = print F_WC_TMP"$line\n";
and $replaceString = $line = join ' ', split ' ', $line; print F_WC_TMP"$line\n";

But when I am running the above command, i think it is replacing the $findstring with the above mentioned string and hence it contains a $line, it is looking for the variable $line and not finding the exact string. I am confused about how to search for a string that contains $ in it and replace it with another $string. Please help me on this
 
Old 02-15-2011, 02:12 AM   #2
risu
LQ Newbie
 
Registered: Feb 2005
Location: Helsinki, Finland
Posts: 18

Rep: Reputation: 2
Try to precede the $'s and the \'s with a \ (i.e. escape them), in both findstring and replacestring:

#!/bin/bash
findstring='print F_WC_TMP"\$line\\n";'
replacestring='\$line = join " ", split " ", \$line; print F_WC_TMP"\$line\\n";'
perl -pi -e "s/$findstring/$replacestring/" testfile

Risto
 
1 members found this post helpful.
Old 02-15-2011, 02:16 PM   #3
hsmak_linux
LQ Newbie
 
Registered: Oct 2008
Location: Jonesboro, AR
Distribution: Ubuntu
Posts: 3

Rep: Reputation: 1
Lightbulb

If you understood you, you want a Perl script do you this. Here it is:

================================
PHP Code:
#!/usr/bin/perl
# command: ./thiscode.pl str1 str2 testfile

# @ARGV is the array where arguments from the command line are stored
$str1 $ARGV[0];
$str2 $ARGV[1];

open(FILE$ARGV[2]) or die "Canít open $ARGV[1] : $!\n";

while(
$line = <FILE>){
    
chomp ($line);
    
    print 
"Before substituting: "$line ,"\n";
    
$line =~ s/$str1/$str2/g;    
    print 
"After substituting : "$line ,"\n\n";

==================================

That should do it..
Good luck!
 
1 members found this post helpful.
Old 02-15-2011, 03:53 PM   #4
koundinya749
LQ Newbie
 
Registered: Feb 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Hello,
Thanks a lot. It worked.

I have one more question. Actually I wanted to insert a new line before an already existing line. Thats why I am searching for the existing line and replacing it with a newline;existing line. But Is there any way just to insert a new line before an existing line without replacing it?

Thanks.
 
Old 02-15-2011, 04:02 PM   #5
risu
LQ Newbie
 
Registered: Feb 2005
Location: Helsinki, Finland
Posts: 18

Rep: Reputation: 2
What if you insert a newline character into replacestring instead of space (after the first semicolon):

replacestring='\$line = join " ", split " ", \$line;\nprint F_WC_TMP"\$line\\n";'

Risto
 
1 members found this post helpful.
Old 02-15-2011, 04:52 PM   #6
koundinya749
LQ Newbie
 
Registered: Feb 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Yes. It worked. Thanks a lot.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl command to replace string by line numbe andiramesh_m Linux - General 6 07-15-2010 12:00 PM
Perl find file and then replace string in file moos3 Programming 5 07-29-2009 07:10 AM
Linux command to find and replace string within text file chips11 Linux - Newbie 5 11-24-2008 02:25 PM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 03:07 AM
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 05:44 AM.

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