in reply to Read strings from a file, search and replace same string in another file.

use strict; use warnings; my $f1 = '1.txt'; my $f2 = '2.txt'; my (@strings, $text); open (FH, $f1) || die; chomp(@strings = <FH>); open (FH, $f2) || die; $text = join '', <FH>; $text =~ s/\Q$_\E/******/g for @strings; ### Make sure it's treated as string and not expression open (FH, ">$f2") || die; print FH $text; close FH;

1.txt:

find search

2.txt:

a bunch of words with find and search and some other words

2.txt after run:

a bunch of words with ****** and ****** and some other words

Replies are listed 'Best First'.
Re^2: Read strings from a file, search and replace same string in another file.
by Anonymous Monk on Dec 09, 2011 at 05:06 UTC

    Hi, Thanks for your reply. I was getting stuck with error at "Died at line 9 script.pl" Line number 9 contains open (FH, $f1) || die; statement. I used another file to write the data. Thaks a lot again for your solution. :)