linux - 为什么这个 sed 命令替换行而不是附加到它?

标签 linux bash shell sed

我有一个包含以下行的文件:

some.url=failover:(nio://localhost:12345)?maxAttempts=5&timeout=10000

我想用另一行完全替换这一行:

some.url=failover:(nio://someserver.com:6789)?something=10&another=6

但是下面的 sed 命令附加到该行,而不是替换它,知道我做错了什么吗?

sed -i 's~^some.url=.*$~some.url=failover:(nio://someserver.com6789)?something=10&another=6~'

最佳答案

您需要对替换部分的&进行转义。因为它有特殊的含义(打印匹配的字符串)。

sed 's~^some\.url=.*$~some.url=failover:(nio://someserver.com6789)?something=10\&another=6~' file

示例:

$ echo 'some.url=failover:(nio://localhost:12345)?maxAttempts=5&timeout=10000' | sed 's~^some.url=.*$~some.url=failover:(nio://someserver.com6789)?something=10\&another=6~'
some.url=failover:(nio://someserver.com6789)?something=10&another=6

关于linux - 为什么这个 sed 命令替换行而不是附加到它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26235216/

相关文章:

linux - 如何强制所有请求都来 self 的网站以避免使用 Apache 进行欺骗

linux - 保持 meteor 在亚马逊 EC2 上运行

python - 如何从 python 程序发送信号?

bash - 我如何等待某个进程的某些输出然后在 Bash 中继续?

linux - 在 shell 中提取文件名的一部分

python - 如何从终端运行 FastAPI?

linux - node.js 无法使用/usr/local/lib 中的 lib

bash - 等待 Wine 完成运行

linux - 如何用添加的文本回显一个带有星号的变量?

linux - sh文件没有在cron ubuntu上运行