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?
CodePudding user response:
Instead of directly expanding a shell variable in the perl code, you could pass it to perl as an argument with the s
switch:
#!/usr/bin/env bash
perl -i -spe 's/name\@domain\.org/$replacement/' -- -replacement="$1" file1.txt file2.txt
In perl s///
, without using the e
or ee
modifiers, variables in the replacement part are treated as literals, so you don't need to escape them.
CodePudding user response:
This works, but needs you to pass the new mail address to the script with the @
character preceded by \\
:
#!/bin/bash
perl -pi -e "s/name\@domain.org/$1/" file-list
If the script is subst
, run as:
subst newname\\@example.com