引用和 sed 的问题

发布于 2024-12-06 02:08:40 字数 219 浏览 0 评论 0原文

我正在尝试使用 sed 查找并替换一些文本。具体来说,我试图在可能包含空格的变量周围添加引号。

sed -i 's/$VAR some.file/"$VAR" some.file/g' path/to/file

不幸的是,结果并非预期。我也尝试过反斜杠引号,但没有成功。我在这里缺少什么?

sed命令在Windows/cygwin下执行。

I'm trying to find and replacing some text using sed. Specifically, I'm trying to add quotes around a variable which could contain whitespaces.

sed -i 's/$VAR some.file/"$VAR" some.file/g' path/to/file

Unfortunately, the result is not expected. I've also tried to backslash the quotes, with no luck. What am I missing here?

The sed command is executed under Windows/cygwin.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一袭水袖舞倾城 2024-12-13 02:08:40

您忽略了单引号阻止变量替换的事实:

sed -i "s/$VAR some.file/\"$VAR\" some.file/g" path/to/file

甚至

sed -i $(printf 's/%s some.file/"%s" some.file/g' "$VAR" "$VAR") path/to/file

You're missing the fact that single quotes prevent variable substitution:

sed -i "s/$VAR some.file/\"$VAR\" some.file/g" path/to/file

or even

sed -i $(printf 's/%s some.file/"%s" some.file/g' "$VAR" "$VAR") path/to/file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文