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
\nusing thescommand is an extended feature that not allsedimplementations 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\nmeans a literal newline when it occurs in the replacement text and will insert anninstead. To be portable, you would have to ensure that each literal newline in$replaceis escaped. If you usereplace=${replace//$'\n'/\\$'\n'}in bash, that would do it. – Kusalananda Nov 06 '23 at 18:15--posixflag on my GNUsedto "disable all GNU extensions", it still works with the literal\n(like in my answer) and doesn't insertncharacter. – aviro Nov 06 '23 at 18:29\&means a literal&rather than the matched text). So\nmean, 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\ninserts a newline is hidden away in a single place in theinfodocumentation of GNUsed, so it's at least documented. It's still not portable though. – Kusalananda Nov 07 '23 at 16:25