Issue
I have a script to replace a specific email address in various files. The replacement address is the first parameter to the script:
#!/bin/bash
perl -pi -e s/'name\@domain\.org'/$1/ file-list
This doesn't work, as the @
character in $1
is substituted by perl. Is there a straightforward fix for this? Running the script as subst foo\@bar.com
, subst foo\\@bar.com
, subst "[email protected]"
, and so on, doesn't work. Is there a sed script that could handle this more easily?
Solution
Instead of expanding a shell variable directly in the perl code you can pass it as an argument by setting the s
switch:
#!/usr/bin/env bash
perl -i -spe 's/name\@domain\.org/$replacement/' -- -replacement="$1" file1.txt file2.txt
In perl's s///
, without the use of e
or ee
modifiers, variables in the replacement part are treated as literals so you don't need to escape them.
Answered By - Fravadona Answer Checked By - Mary Flores (WPSolving Volunteer)