I have a PHP file which contains an email address as with $to = "example-1_2@example-1_2.com";
and I want to replace the email address to another directly from the terminal.
The following code based on another similar code by Kuslanada worked:
read new_email_address
sed -i 's/$to = ".*";$/$to = "'"$new_email_address"'";/' FILE
Kuslananda told me in comments (paraphrasing):
- My expression is the concatenation of a single-quoted string, a double-quoted string (the variable), and then another short single-quoted string
As a non professional sysadmin I might use sed
once in two years and I find its syntax somewhat difficult to remember and confusing for someone like me which doesn't work with it on a regular basis (the quoting rules clued by Kuslananda there are a bit confusing for me, let along when entire sed
commands are normally single-quoted by themselves).
Perhaps there is a way to make Kusalananda's command more "intuitive" or "accessible" for some others and myself. Maybe some backslashes to break the command to pieces would help or maybe another utility would be better for me to do such text replacement.
How to replace an email in a (PHP) file from the command line without sed
?
$
in there, what is it supposed to be doing?*$
means anything that ends with [CHARACTERS]. Of course I was wrong. I think I should delete this code; I saw you answered aboutsed
but I need a way to do essentially what Kusalananda did but without sed because the syntax ofsed
is just to hard to me to remember as someone who doesn't usesed
frequently (rather, once in a few years).^
and$
in non-obvious places), the second one is mine.$
matches a$
in a BRE, unless it's the last character in an expression or a sub-expression, in which case it matches the end of the line.$
in the line after the first character (note that I was referring to the first version of the post).sed 's/old/new/'
. It is only complicated because you want to use a shell variable.