[英]Sed sed: -e expression #1, unterminated `s' command
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我正在尝试使用Renovate运行此 sed 命令,我认为该工具并不重要,但我收到以下错误
"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请注意,它在 sed 表达式(一些文本)中没有空格的情况下工作正常,这意味着以下工作正常
"find . -name release-notes.md -type f -exec sh -c 'sed -i '1s#^#sometext#' \"$1\"' sh {} \\;",
Thanks谢谢
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.在表达式sh -c 'sed -i '1s#^#some text#' \"$1\"'
中,第二个单引号(紧接在“1s”之前的那个)终止左引号和字符串1s#^#some text#
未被引用。 It's very common to see the script argument to sed
quoted with single quotes, but it's not at all necessary. sed
的脚本参数用单引号引起来是很常见的,但这根本不是必需的。 In your case, the 2 simplest solutions are:在您的情况下,2 个最简单的解决方案是:
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. -e
不是绝对必要的,但非标准-i
在某些版本中需要备份后缀,我发现明确使用-e
以避免混淆更清晰。
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?不确定为什么调用 sh,-exec 将使用默认值 shell,这应该可行吗? unless you have an edge case?除非你有一个边缘案例?
Correct me if I am wrong, but I think this will work.如果我错了,请纠正我,但我认为这会奏效。
#!/bin/sh -x
find . -name release-notes.md > stack
while read line
do
echo "some text" > newfile
sed -n '2,$p' "${line}" >> newfile
mv -v newfile "${line}"
done < stack
I personally dislike using -exec with find.我个人不喜欢将 -exec 与 find 一起使用。 I feel that find commands are frequently complex enough as it is, without trying to jam everything into a single line.我觉得 find 命令通常已经足够复杂了,而不是试图将所有内容都塞进一行。 I frequently see problems with find exec lines which I am able to bypass with methods such as the above.我经常看到 find exec 行的问题,我可以使用上述方法绕过这些问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.