I have
#!/usr/bin/bash
search="ldo_vrf18 {"
replace="$search"' compatible = "regulator-fixed";
regulator-name = "vrf18";
regulator-min-microvolt = <1800000>;
regulator-max-microvolt = <1800000>;
regulator-enable-ramp-delay = <120>;'
sed -i "s/$search/$replace/g" output.file
The result is
sed: -e expression #1, char 62: unterminated `s' command
I suspect some values arent being escape in replace
. Is there a way to escape them?
I have tried sed -i 's/'"$search"'/'"$replace"'/g' output.file
with the same result
\n
using thes
command is an extended feature that not allsed
implementations have. To do it portably, escape each literal newline with a backslash. – Kusalananda Nov 06 '23 at 17:59replace=${replace//$'\n'/\\$'\n'}
? – aviro Nov 06 '23 at 18:15sed
(as found on e.g. some BSD systems) does not understand that\n
means a literal newline when it occurs in the replacement text and will insert ann
instead. To be portable, you would have to ensure that each literal newline in$replace
is escaped. If you usereplace=${replace//$'\n'/\\$'\n'}
in bash, that would do it. – Kusalananda Nov 06 '23 at 18:15--posix
flag on my GNUsed
to "disable all GNU extensions", it still works with the literal\n
(like in my answer) and doesn't insertn
character. – aviro Nov 06 '23 at 18:29\&
means a literal&
rather than the matched text). So\n
mean, literally, "an ordinary n". It also says: "A line can be split by substituting a newline into it. The application shall escape the newline in the replacement by preceding it by a backslash." – Kusalananda Nov 06 '23 at 18:39'\'
immediately followed by any character other than '&
','\'
, a digit, or the delimiter character used for this command, is unspecified. See also this bug report. – aviro Nov 07 '23 at 16:21\n
inserts a newline is hidden away in a single place in theinfo
documentation of GNUsed
, so it's at least documented. It's still not portable though. – Kusalananda Nov 07 '23 at 16:25