Sorry. Just had an error pointed out. Remove the i, not the g from the line
for case sensitity. Not thinking straight...

g is for global matches, so if your search string appears more than once on
each line of your input file, all instances will be converted to your
replacement string. If the g isn't there then only the first match on each
line will be replaced.

John

-----Original Message-----
From: John Edwards [mailto:[EMAIL PROTECTED]]
Sent: 30 July 2001 15:24
To: 'alex stan'; [EMAIL PROTECTED]
Subject: RE: String replace in a file


$lookfor = "string";
$replacewith = "newstring";

open IN, "input.txt" or die "Can't open input.txt: $!";
open OUT, ">output.txt" or die "Can't create output.txt";

while (<IN>) {
        s/$lookfor/$replacewith/ig;
        print OUT;
}

close OUT;
close IN;

output.txt should now have all instances of $lookfor replaced with
$replcaewith.

Note, it's case insensitive, if you don't want that, remove the g from
s/$lookfor/$replacewith/ig;

HTH

John

-----Original Message-----
From: alex stan [mailto:[EMAIL PROTECTED]]
Sent: 30 July 2001 15:17
To: [EMAIL PROTECTED]
Subject: String replace in a file


Could anyone help me?
How can i find a string in a file and then replace it with another string?
Thanks a lot





---
ROL!RO free mail - http://mail.rol.ro
Spatiu nelimitat pentru contul tau si acces gratuit prin dial-up!


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------Confidentiality--------------------------.
This E-mail is confidential.  It should not be read, copied, disclosed or
used by any person other than the intended recipient.  Unauthorised use,
disclosure or copying by whatever medium is strictly prohibited and may be
unlawful.  If you have received this E-mail in error please contact the
sender immediately and delete the E-mail from your system.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------Confidentiality--------------------------.
This E-mail is confidential.  It should not be read, copied, disclosed or
used by any person other than the intended recipient.  Unauthorised use,
disclosure or copying by whatever medium is strictly prohibited and may be
unlawful.  If you have received this E-mail in error please contact the
sender immediately and delete the E-mail from your system.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to