【问题标题】:Replace string by sed with double quota (special characters)用双倍配额(特殊字符)用 sed 替换字符串
【发布时间】:2022-01-26 13:00:04
【问题描述】:

我想在 RHEL6 中将 fileSystem: "/test/test" 更改为 fileSystem: "/apptest",我尝试使用:

sed -i 's/fileSystem: "/test/test"/fileSystem: "/apptest"' text.txt

但不起作用,输出:

sed: -e expression #1, char 26: unknown option to `s'

我不知道如何更改它。我阅读了一些文件,但不知道如何工作。

你能帮帮我吗?

问候。

【问题讨论】:

  • 更改路径中的 sed 分隔符或转义 le /

标签: linux bash shell unix


【解决方案1】:

因为您的字符串本身包含/ 字符,您应该为s 命令使用不同的分隔符。例如#:

sed -i 's#fileSystem: "/test/test"#fileSystem: "/apptest"#' text.txt

(替换后您还缺少分隔符。)

【讨论】:

  • 谢谢它的工作:)。还有一个问题 - 我想输入如下读取命令:read fs?" 请提供路径名:" sed -i 's#fileSystem: "/test/test"#fileSystem: "$fs"#' text.txt 但不起作用.你能建议我如何在上面的 sed 推荐中使用变量吗?我应该以某种方式更改以上命令吗?
  • 变量不在单引号内展开。将单引号 ' 替换为双引号 ",然后使用 \" 将双引号 inside 转义。
猜你喜欢
  • 2021-11-17
  • 1970-01-01
  • 2019-10-14
  • 1970-01-01
  • 2019-09-07
  • 2021-06-11
  • 1970-01-01
相关资源
最近更新 更多