Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > making replacements in a file

Reply
Thread Tools

making replacements in a file

 
 
Jeff Thies
Guest
Posts: n/a
 
      07-11-2003
I have a file that I need to make some substitutions in:

open FH, "my_file" or die "Can't open... $!";

@file=<FH>;

Can I read the file handle so it is a scalar and I can just do this?:

$file=~s/replace_me/with_this/gis; # small files

and then print back to a filehandle.

Or should I do something like this:

my @new_file=();
while(<FH>){
$_=~s/replace_me/with_this/gi;
push @new_file, $_;
}

and then print @new_file to a filehandle? Or would that be a reference
to @new_file?

Seems like there should be a better way than any of these.

Jeff
 
Reply With Quote
 
 
 
 
Bob Walton
Guest
Posts: n/a
 
      07-11-2003
Jeff Thies wrote:

> I have a file that I need to make some substitutions in:
>
> open FH, "my_file" or die "Can't open... $!";
>
> @file=<FH>;
>
> Can I read the file handle so it is a scalar and I can just do this?:



Sure. Slurp it. Set the value of $/ to undef, perhaps like:

{local $/; #make $/ undef only for the duration of this block,
#so it doesn't screw up possible subsequent reads.
$file=<FH>; #slurp entire file to scalar.
}

This sets the input record separator string to undef, which is a special
case that causes file "slurping". See:

perldoc perlvar

for more info.


>
> $file=~s/replace_me/with_this/gis; # small files
>
> and then print back to a filehandle.
>
> Or should I do something like this:
>
> my @new_file=();
> while(<FH>){
> $_=~s/replace_me/with_this/gi;
> push @new_file, $_;
> }
>
> and then print @new_file to a filehandle? Or would that be a reference
> to @new_file?



That, of course, would also work, unless the "replace_me" string
contains stuff which is supposed to match over a newline. The fact that
you included the "s" regexp modifier in your previous "slurp version"
regexp indicates that perhaps you intended for that to be the case
(otherwise, "s" would serve no purpose).

Of course, you don't really have to push all the lines out to an array
in the above -- just write out the modified lines as you read them.
That gives your program the distinct advantage of being able to process
a gigabyte file on your old 386 with 16 Mb of memory. Just replace the
push line with something like:

print OUT $_;

and place something like:

open OUT,">output.file" or die "Oops, $!";

before the while loop.


>
> Seems like there should be a better way than any of these.



There's always more than one way to do it. Which is "better" depends on
a lot of stuff you haven't defined for us, as well as your opinion of
what "better" means.


>
> Jeff
>


--
Bob Walton

 
Reply With Quote
 
 
 
 
Mina Naguib
Guest
Posts: n/a
 
      07-11-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jeff Thies wrote:
> I have a file that I need to make some substitutions in:
>
> open FH, "my_file" or die "Can't open... $!";
>
> @file=<FH>;
>
> Can I read the file handle so it is a scalar and I can just do this?:
>
> $file=~s/replace_me/with_this/gis; # small files
>
> and then print back to a filehandle.
>
> Or should I do something like this:
>
> my @new_file=();
> while(<FH>){
> $_=~s/replace_me/with_this/gi;
> push @new_file, $_;
> }
>
> and then print @new_file to a filehandle? Or would that be a reference
> to @new_file?


You can let perl worry about opening the files, writing back to them, and even looping over the
lines, through command-line switches. This program does what you're doing above:

- -- cut here --
#!/usr/bin/perl -pi

s/replace_me/with_this/gi;
- -- cut here --

That's it ! For more info on the -p and the -i switches (and many other interesting ones), see
perldoc perlrun


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/DkOteS99pGMif6wRAvrYAKD/oylKq/WL6pLBixWlA9VCng542QCfY/dt
BbWHBtt+1GK7PCGVriMaMuA=
=6jHE
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
web config file section replacements ThatsIT.net.au ASP .Net 3 09-27-2008 02:26 AM
Isolate lines in a text file and perform replacements Robert Neville Perl Misc 4 04-03-2007 06:10 PM
Web Deployment Project and web.config file section replacements?? Brian McCullough ASP .Net 0 02-16-2007 09:55 PM
Sony InfoLithium Battery Replacements Mike Schudel Digital Photography 2 07-22-2004 12:52 AM
Replacements for BP-511 for Canon cameras Rick Langston Digital Photography 12 03-07-2004 04:03 PM



Advertisments