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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modify a file string in place

Status
Not open for further replies.

sedawk

Programmer
Joined
Feb 5, 2002
Messages
247
Location
US
Hello,

I want to open a file, find a target string, then change the string to something else, and finally save the file. The code opens the file with ">+". However, rather than modifying the string in place, it adds another line. I wonder what is the right syntax. Here is an example of what I want to do:

===== the file: text.txt =====
parameters tmp=95

This file should be changed to
====== after the Perl script =====
parameters tmp=27

Nothing else should be changed after running the script. The script is as follow. It adds another line, which is not what I want.

Code:
$temp = 95;
$newtmp = 27;
$file = "text.txt";
                                                                                                                            
open(FILE, "+< $file") or die "no file has been opened";
                                                                                                                            
@contents = <FILE>;
                                                                                                                            
foreach(@contents) {
        if(/parameters tmp=(\d+)/) {
                if ($1==95) {
                         s/$1/$newtmp/eg;
                         print FILE $_;
                }
        }
}
close(FILE);
 
I'm guessing that your file is not very large. I therefore suggest that you just write out the entire file again, as this would prevent any limitations with changing the number of characters in the line you are modifying:

Code:
use strict;

my $temp = 95;
my $newtmp = 27;
my $file = "text.txt";

# Slurp File
open(INPUT, "$file") or die "can't open $file: $!";
my @contents = <INPUT>;
close(INPUT) or die "can't close $file: $!";

# Update Contents
foreach (@contents) {
	s/(?<=parameters tmp=)$temp/$newtmp/;
}

# OverWrite File
open(OUTPUT, "> $file") or die "can't open $file: $!";
print OUTPUT $_ foreach (@contents);
close(OUTPUT) or die "can't close $file: $!";

There is little to no reason to become fixated on in place updating of the file, unless the above does not work for some reason. If it's a learning exercise, that's something else again though. But if that's the case, the best thing to do would be to just open up the Perl Cookbook and work it out on your own.
 
Code:
$temp = 95;
$newtmp = 27;
$file = "text.txt";
{
   local ($^I, @ARGV) = ('.bak', $file);
   while (<>) {
      if (s/parameters tmp=$temp/parameters tmp=$newtmp/) {
         print;
      }
      else {
         print;
      }
   }
}

- Kevin, perl coder unexceptional!
 
or as a one-liner:

Code:
perl -pi.bak -e "s/parameters tmp=95/parameters tmp=27/" text.txt

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top