CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Advanced: Problem replacing only 1 peice of information in a text file! Please help.: Edit Log

New User

Apr 28, 2002, 3:22 AM

Views: 4371
Problem replacing only 1 peice of information in a text file! Please help.

Hello everyone,

I have a problem and I cannot seem to fix it. What I am
trying to do is open a text file which contains the follwing


And so on. Now what the script is supossed to do is read
in this file then open another file on the system and do
a search and replace according to the list above.

If it find OLDTHING it will replace it with NEWTHING same
for OLDTHING2 and so on.

What is happening it skips every other line in the file it
is seearching and it moves the text to the next line.

Now the search file may look like this:

This is the text and I want to replace 154.25 with 125.25

or may look like so:

And only need to replace the first same as above. Here
is the code I am using now we get the file name from
user input.


# Enter the name of the file with the stuff to change
my $chglist = "changelist";

# lets promtpt for a file name
print "Enter file name: ";
chomp($in = <STDIN>);

if($in ne "") {
open(CHGFILE, $chglist) or die "Can't open $chglist: $!\n";
while(<CHGFILE>) {
# Here we get all the stuff and add them to easy to use variables
($oldstuff, $newstuff) = split("=", <CHGFILE>);
foreach ($_) {
$old = $in;
$new = "$file.tmp.$$";
$bak = "$file.orig";

open(OLD, "< $old") or die "can't open $old: $!";
open(NEW, "> $new") or die "can't open $new: $!";

# Correct typos, preserving case
while (<OLD>) {
(print NEW $_) or die "can't write to $new: $!";

close(OLD) or die "can't close $old: $!";
close(NEW) or die "can't close $new: $!";

rename($old, $bak) or die "can't rename $old to $bak: $!";
rename($new, $old) or die "can't rename $new to $old: $!";
print "Enter file name: ";
chomp($in = <STDIN>);

Thanks in advance.


(This post was edited by CWDSolutions on Apr 28, 2002, 3:23 AM)

Edit Log:
Post edited by CWDSolutions (New User) on Apr 28, 2002, 3:23 AM

Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives