[gnu-sed-devel] Re: & commercial replace problems


On Tue, Mar 21, 2017 at 3:11 AM, Mickael MONSIEUR
 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 "" > test
> APIKEY="test&test&test"
> sed  -i -e "s//$APIKEY/g" test
> cat test
> rm test
>
> Result:
>
> root@local:/tmp# ./test.sh
> testtesttest
>
> 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



This message from: http://www.mailbrowse.com/gnu-sed-devel/248.html
Previous message: Patch for sed-4.2.2.177-a348 on mingw-w64
Next message:Suggestion: tests: refactor tests to new init.sh-style shell scripts