in reply to search and replace if pattern found in file
use warnings; use strict; my $old = quotemeta "welcome|to|chennai"; my $new = "Capital of karnataka|is|bangalore"; my $base = (); my @base = (); @base = <DATA>; chomp @base; foreach $base (@base) { if ( $base =~ /$old/ ) { $base =~ s/$old/$new/gi; print("Replaced!\n"); } print @base; } __DATA__ welcome|to|chennai
The | is a special character in regexes (see perlre). It is used for alternation. The s///g modifier was replacing "welcome" with your $new string, then replacing "is" with your $new string, then replacing "chennai" with your $new string.
I've also chomped your input.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: search and replace if pattern found in file
by myfrndjk (Sexton) on Jul 31, 2014 at 14:29 UTC |