You don't have to write it to another file.  You can do something like this,
and it will work.

open(INFILE,"textfile.txt");    #open file for input
@infile = <INFILE>;             #put contents of file into array
close INFILE;
foreach $line(@infile){
        $line =~ s/foo/bar/gi;  #do replace for each line
}
open(OUTFILE,">textfile.txt");#open file for output
print OUTFILE @infile;          #print changed array to file



-----Original Message-----
From: Dirk Bremer [mailto:[EMAIL PROTECTED]]
Sent: Thursday, December 13, 2001 2:32 PM
To: [EMAIL PROTECTED]
Subject: Re: Find/Replace Question


Eric,

As an additional note, on some platforms (Windoze included), to make this
change work, you will have to open the original file as
input and write all lines, including the changed lines, to another file that
you will open as output. You can replace the original
file with the rename function from within Perl or use other methods, i.e.
DOS commands, etc.

Dirk Bremer - Systems Programmer II - AMS Department - NISC
636-922-9158 ext. 652 fax 636-447-4471

<mailto:[EMAIL PROTECTED]>

----- Original Message -----
From: "Timothy Johnson" <[EMAIL PROTECTED]>
To: "'Eric Westrom'" <[EMAIL PROTECTED]>;
<[EMAIL PROTECTED]>
Sent: Thursday, December 13, 2001 4:23 PM
Subject: RE: Find/Replace Question


>
> For this you will want to use the substitution operator.  The general
syntax
> is:
>
> $scalar =~ s/$find/$replace/;
>
> E.g.
> $scalar = "FooBar";
> $scalar =~ s/Bar/Bear/;
> print $scalar;
>
> will print 'FooBear'.
>
> In addition two of the most frequently used switches (placed after the
third
> slash) are g and i.  'g' will replace all instances (global), and 'i' will
> do a case-insensitive search.
> "$scalar =~ s/bar/Bear/i" will replace 'bar', 'BAR', or 'Bar', while
without
> the 'i', it will only replace 'bar'.  If you leave out the 'g' switch, it
> will only replace the first instance of the pattern match.
>
> Hope this helps...
>
> -----Original Message-----
> From: Eric Westrom [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, December 13, 2001 1:39 PM
> To: [EMAIL PROTECTED]
> Subject: Find/Replace Question
>
>
> Let me preface with "Sorry to ask a dumb question but..."
>
> With that said, I'm quite new to perl and I wanted to see if there was a
way
>
> to search a text file for a particular string and replace it with a
> different value. The end result would be an updated text file on disk with
> the necessary changes.
>
> Example:
>
> file.txt exists on the drive.
> Scalar $Server="Win2kDC1"
>
> File.txt contains a string %SRV%. I want to search for every occurence and
> replace with $Server. File.txt would be updated in place if possible.
>
> I have done this before with vbscript and RegExp, but I am new to perl and
> do not know the syntax. Any help would be greatly appreciated. Again,
sorry
> for the newbie question.
>
> Eric
>
> _________________________________________________________________
> Send and receive Hotmail on your mobile device: http://mobile.msn.com
>
> _______________________________________________
> Perl-Win32-Admin mailing list
> [EMAIL PROTECTED]
> http://listserv.ActiveState.com/mailman/listinfo/perl-win32-admin
>
>
>
----------------------------------------------------------------------------
----
> This email may contain confidential and privileged
> material for the sole use of the intended recipient.
> If you are not the intended recipient, please contact
> the sender and delete all copies.
> _______________________________________________
> Perl-Win32-Admin mailing list
> [EMAIL PROTECTED]
> http://listserv.ActiveState.com/mailman/listinfo/perl-win32-admin
>
>

_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-admin


--------------------------------------------------------------------------------
This email may contain confidential and privileged 
material for the sole use of the intended recipient. 
If you are not the intended recipient, please contact 
the sender and delete all copies.
_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-admin

Reply via email to