Comments on: Using shell variables in sed https://backreference.org/2009/12/09/using-shell-variables-in-sed/ Proudly uncool and out of fashion Fri, 20 Feb 2015 17:41:29 +0000 hourly 1 https://wordpress.org/?v=5.8.2 By: waldner https://backreference.org/2009/12/09/using-shell-variables-in-sed/#comment-25173 Fri, 20 Feb 2015 17:41:29 +0000 http://backreference.org/?p=3#comment-25173 In reply to Kusuma.

Your problem has nothing to do with sed, but rather with your shell. In any case, the single quote must NOT be escaped, as it's a perfectly normal character for sed.

If you want to know how to insert a single quote in a single-quoted string, see for example this page: http://wiki.bash-hackers.org/syntax/quoting#strong_quoting

]]>
By: Kusuma https://backreference.org/2009/12/09/using-shell-variables-in-sed/#comment-25172 Fri, 20 Feb 2015 16:58:48 +0000 http://backreference.org/?p=3#comment-25172 How to escape single quote in RHS?

I tried like this
safe_replacement_oracle=$(printf '%s\n' "$ORACLE_PASS" | sed 's/[\&/'"]/\\&/g')

ORACLE_PASS is !_#%1

/cygdrive/D/KonyServer1/Bash_Scripts/User_Input.properties.bash: line 22: syntax error near unexpected token `('
/cygdrive/D/KonyServer1/Bash_Scripts/User_Input.properties.bash: line 22: `ORACLE_PASS='!_#%1''

]]>
By: waldner https://backreference.org/2009/12/09/using-shell-variables-in-sed/#comment-25047 Tue, 25 Feb 2014 11:02:48 +0000 http://backreference.org/?p=3#comment-25047 In reply to pierocampa.

So you have a variable whose value is, to continue with the last example

X  \n  Y

and it should become, to be used in the RHS,

X  \  \n  Y

(plus the other normal RHS escaping).

Since sed doesn't see newlines directly, all you have to do is just to put a backslash at the end of each line (except the last), so you can do:

$ repl1=$'X\nY'
$ printf '%s' "$repl1" | od -c
0000000   X  \n   Y
0000003
$ repl2=$(printf '%s' "$repl1" | sed 's/[\&/]/\\&/g; $!s/$/\\/')    # the trick is the second s/// command
$ printf '%s' "$repl2" | od -c
0000000   X   \  \n   Y
0000004
$ sed "s/blah/$repl2/" file ...

Hope this helps.

]]>
By: pierocampa https://backreference.org/2009/12/09/using-shell-variables-in-sed/#comment-25046 Tue, 25 Feb 2014 09:55:16 +0000 http://backreference.org/?p=3#comment-25046 In reply to waldner.

Thanks for you quick comments. !
I used `eval' because my replacement is yielded by the execution of a command (namely a `cat').

I do not mind dropping the newlines in my specific application, but just in case: how would I escape a newline with sed?
Tried with $ ans \n but does not work:

> sed 's/[\&/$]/\\&/g'
> sed 's/[\&/\n]/\\&/g'

This is my $replacement by the way:

{{{

Coordinate system axis for the recording of days [d].
http://www.opengis.net/def/axis/OGC/0/days
day
http://www.opengis.net/def/axisDirection/OGC/1.0/future

}}}

Again: it works out by escaping [\&/] (no need for quotes and dots, you were right), and dropping newlines \n.

]]>
By: waldner https://backreference.org/2009/12/09/using-shell-variables-in-sed/#comment-25045 Tue, 25 Feb 2014 09:39:46 +0000 http://backreference.org/?p=3#comment-25045 In reply to pierocampa.

To have a literal newline in the RHS you have to escape it. Example follows without variables:

$ echo foobar | sed 's/foo/X\
Y/'
X
Ybar

So if you use a variable, it must end up with the value (characters spaced for clarity, \n is a literal newline character)

X  \  \n  Y

Example:

$ repl=$'X\\\nY'
$ printf '%s' "$repl" | od -c
0000000   X   \  \n   Y
0000004
$ echo foobar | sed "s/foo/$repl/"
X
Ybar
]]>