[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: & commercial replace problems
From: |
Jim Meyering |
Subject: |
Re: & commercial replace problems |
Date: |
Tue, 21 Mar 2017 09:36:58 -0700 |
On Tue, Mar 21, 2017 at 3:11 AM, Mickael MONSIEUR
<address@hidden> wrote:
> I have a problem with the replace when a "&" commercial is present.
> Because "&" it is used in URL. (page.php?hello=world&john=doe)
>
> Example:
>
> echo "<apikey>" > test
> APIKEY="test&test&test"
> sed -i -e "s/<apikey>/$APIKEY/g" test
> cat test
> rm test
>
> Result:
>
> address@hidden:/tmp# ./test.sh
> test<apikey>test<apikey>test
>
> The replacement text (search) should not be totally insensitive to regular
> expressions?
"&" has a special meaning in the right hand side of a sed replacement
expression (it expands to the text matched by the regexp in the LHS).
If you want a literal ampersand ("&"), you should quote it. Compare
the following.
# Using unescaped "&" on the RHS, it is replaced by "b":
$ echo abc | sed 's/b/b&/'
abbc
# Escape it, and you get the literal:
$ echo abc | sed 's/b/b\&/'
ab&c
This is well documented in the manual:
https://www.gnu.org/software/sed/manual/html_node/The-_0022s_0022-Command.html#The-_0022s_0022-Command