Re: replace existing text within multiple text files: How?

2007-09-11 Thread Xavier Noria

On Sep 11, 2007, at 9:43 PM, Gerald Wheeler wrote:


I have about 400 text files I need to replace the contents. All files
are in the current directory as the perl script runs out of

my $newText = This is my new text.. anybody's text goes here;

open(INFILE, $plants)
while ()
{
 print $newText;
}

I need help..


You mean $newText contains the whole content of a single file, and  
the 400 files end up having that very same content (that is $newText)?


-- fxn


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: replace existing text within multiple text files: How?

2007-09-11 Thread Chas Owens
On 9/11/07, Gerald Wheeler [EMAIL PROTECTED] wrote:
 I have about 400 text files I need to replace the contents. All files
 are in the current directory as the perl script runs out of
snip

What you need to do depends heavily on what you mean by replace the
contents.  In general you can get away with something like

perl -pi.bak -e 's/old stuff/new stuff/' *

This will rename the existing files to whatever their name is plus
.bak and then run the substitution against them storing the output
in the original file names.  The expanded script version of this is

#!/usr/bin/perl

use strict;
use warnings;

#set the inplace edit flag
#this also has the effect of opening
#and calling select on each file in @ARGV
$^I = .bak;

while () {
s/old stuff/new stuff/;
print;
}

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: replace existing text within multiple text files: How?

2007-09-11 Thread Xavier Noria

On Sep 11, 2007, at 10:05 PM, Gerald Wheeler wrote:


Correct


That can be done with a one-liner:

  perl -0777 -pi.bak -e '$_ = q{new text goes here}' *.txt

The options -p, -i, and -e are documented in perlrun. The flag -0777  
has the side-effect of slurping the whole file into $_ (one file at a  
time), it is documented in perlrun as well, under -0.


-- fxn


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/