LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 12-30-2009, 03:10 PM   #1
GuerillaSquad
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Rep: Reputation: 0
Smile How do I Find and Replace text within files using Perl (no one-liners please)


Hi,

I have a Perl script I am trying to write. In all the examples I see of filehandle the infile and outfile are different. ARGV[0] and [1] for example.

I am currently running a File::Find in my script and when I call the find method I want it to be able to open a found file, search the file for "cat", replace it with "dog", close the file.

I have two questions:
1. Do I have to OPEN the file for READ, close it and then OPEN it for WRITE in two seperate statements?

2. How do I do the following: ?

------------
For example:
------------
Filename is foo
* foo currently has "mike" and "bob" in it for contents.

If I open the file like this >>Larry,
I will get "mike" "bob" "larry"

If I open the file like this >Larry
I will get "larry"

What I want is to replace "bob" with "larry"
I would get "mike" "larry"

I plan to use s/bob/larry/g to do the work but how do I open the file without deleting content?

Thank you,

Last edited by GuerillaSquad; 12-30-2009 at 03:23 PM. Reason: Title change
 
Old 12-30-2009, 04:52 PM   #2
SethsdadtheLinuxer
Member
 
Registered: Jun 2007
Posts: 152

Rep: Reputation: 37
#1 is yes. Perl can only handle the file in one mode at a time. here's an easy way to do it
(hope the indentation isn't crappy)
use File::Copy;
my $filein = $ARGV[0];
my $filetmp = $filein.'_'.$$;
open (my $fh_in, "<", $filein) or die;
open (my $fh_out, ">$filetmp") or die;
while (<$fh_in>) {
my $x = $_;
$x =~ s/bob/joe/g;
print $fh_out $x;
}
close ($filein);
close ($filetmp);
move $filetmp $filein;
exit;
 
Old 12-30-2009, 08:34 PM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Sethsdad's method works very well, and it's such a common desire that there's a built-in way that simplifies things a bit.

If you set the $^I default variable and use the <> operator, you can edit the file in place and get a backup for free. Behind the scenes, Perl takes care of duping the file safely and making the backup for you:

Code:
# Set the suffix for backups to '.bak' and turn on in-place editing
$^I = '.bak'

while (<>) {
    s/bob/larry/g;
}
That's it. The only trick is that when you run this, Perl will perform the substitution on whatever files you specify on the command line. So if the file you want to edit is data.txt, save this script as editor and then you call it as perl editor data.txt.
 
Old 12-31-2009, 09:01 AM   #4
GuerillaSquad
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Hi there. Thanks for the help.
 
  


Reply

Tags
open, perl



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 replace text in file ShaqDiesel Programming 27 08-11-2010 10:50 PM
Perl find file and then replace string in file moos3 Programming 5 07-29-2009 07:10 AM
how to replace string by other one in a file & without perl ? Xeratul Programming 4 05-25-2007 11:13 PM
Perl: replace character in file vippie Programming 4 03-23-2007 04:26 AM
Perl - Tpl file - Need to replace new line character. knnirmal Programming 2 09-07-2004 02:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:42 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration