0

我有一个配置文件,其中有一行ServerIP=。现在我想使用 find 这一行并为其添加一个新的 IP 地址,即用 替换它ServerIP=192.168.0.101,命令是什么样的?

4

4 回答 4

2

您可以使用查找和替换命令来执行此操作:

sed -e 's/\(^ServerIP=\)/\1192.168.0.101/g' your_file

我们是在整个文件中这样做还是只在一个地方这样做?上面的命令应该在任何地方替换它。您必须将输出发送到某处。我从不就地编辑,sed因为我犯了太多错误。

一件棘手的事情是这部分,\1192.168.0.101实际上可以这样分解:

\1 --> the thing we captured
192.168.0.101 --> the thing we are placing IMMEDIATELY after the thing we captured

此外,您可能还有其他看起来有些不同的线条。但是,将来,请查找“sed 捕获和替换”。

于 2013-08-09T06:57:58.090 回答
1

无论 ServerIP 中是否存在现有值,这个都可以工作:

sed -i 's@\([[:blank:]]*ServerIP=\)[[:digit:].]*@\1192.168.0.101@' file

我还建议您尝试使用 VIM 或 Nano 等 CLI 编辑器来学习。

于 2013-08-09T07:13:51.430 回答
0

尝试:

sed -i 's/^ *ServerIP=/&192.168.0.101/' file
于 2013-08-09T06:58:12.570 回答
0

我会做:

sed -i 's/^ServerIP=$/ServerIP=192.168.0.101/' file.config
于 2013-08-09T07:01:31.833 回答