Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Beware Newbie Q: Quick script to search and replace.

Status
Not open for further replies.

awolff01

Programmer
Jun 17, 2003
55
0
0
US
Hello All,

I am a newbie. I need a quick script to read in a file and replace certain text with new text. Is it possible to read and write to the same file?

My current script has this in it. but when I ran it it did not do squat!!

=======script code========
open(CS, "$currscript");
while(<CS>) {
$_ =~ s/QAS/MSP6GA/;
}
===========================

Or do I need to write out to another file with the replaced text?

Thanks in Advance!

Alex-
 
The easiest way IMO is the write out to a temp file then destroy the old file and rename the temp.
 
Try this and modify it for what you are doing:


Code:
use strict;
my $db = 'yourfilehere.txt';
open(DATA, &quot;$db&quot;) || die &quot;Can not open: $!\n&quot;;
my @dat = (<DATA>);
close(DATA);
open(DATA, &quot;>$db&quot;) || die &quot;NO GO: $!\n&quot;;
                                     
foreach (@dat)
{
	s/QAS/MSP6GA/gi;
	print DATA $_;  #output to file
}
close(DATA);

=============
Bad Company Music
=============
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top