I'm trying to run this sed command using Renovate, the tool doesn't really matter I think but I'm getting the following error
"find . -name release-notes.md -type f -exec sh -c 'sed -i '1s#^#some text#' \"$1\"' sh {} \\;",
"stderr":"/bin/sh: 1: Syntax error: Unterminated quoted string\n",
Note that it works fine without the space within the sed expression (some text), meaning that the following works fine
"find . -name release-notes.md -type f -exec sh -c 'sed -i '1s#^#sometext#' \"$1\"' sh {} \\;",
Thanks
CodePudding user response:
In the expression sh -c 'sed -i '1s#^#some text#' \"$1\"'
, the second single quote (the one immediately before "1s") terminates the opening quote and the string 1s#^#some text#
is unquoted. It's very common to see the script argument to sed
quoted with single quotes, but it's not at all necessary. In your case, the 2 simplest solutions are:
sh -c 'sed -i 1s#^#some\ text# \"$1\"'
and
sh -c 'sed -i \"1s#^#some text#\" \"$1\"'
But you could simplify everything by avoiding the unnecessary layer of quoting and doing:
-exec sed -i -e '1s#^#some text#' {} \;
The -e
isn't strictly necessary, but the non-standard -i
wants a backup suffix in some versions, and I find it cleaner to explicitly use -e
to avoid confusion.
CodePudding user response:
find . -name release-notes.md -type f -exec sed -i '1s/\(some text\)/\"\1\"/' {} \;
Using a capture group is often easier then a look behind (or ahead). The above code worked for me. Unsure why you calls sh, -exec will use the default shell, and that should work? unless you have an edge case?