Paresh Gandhare
Paresh Gandhare

Reputation: 33

replace string with an variable having special chars in it using Perl

I have a file with below content:

$ cat file
my name is PARESH.

Need to replace PARESH with content of variable `$REPLACE_WORD" using Perl.

perl -pi -e "s/PARESH/$REPLACE_WORD/g"  file

Problem is variable $REPLACE_WORD has special characters in it.

$REPLACE_WORD="c@sc9ey"

Upvotes: 0

Views: 56

Answers (2)

user7818749
user7818749

Reputation:

When using double quotes, then escape the special characters using \

$REPLACE_WORD = "c\@sc9ey";

Upvotes: 1

simbabque
simbabque

Reputation: 54333

Perl interpolates scalars ($foo) and arrays (@bar) in double quotes "". If you use single quotes '' instead, no interpolation happens.

$REPLACE_WORDS = 'c@sc9ey';

Note that hash variabels (%baz) are not interpolated.

Upvotes: 4

Related Questions