简体   繁体   English

从文件中替换值的 sed 命令是什么?

[英]What is the sed command to replace value from a file?

What is the sed command to replace a below line from a file.从文件中替换以下行的 sed 命令是什么。 I was using below sed command to replace a line, But it didn't work我使用下面的 sed 命令来替换一行,但它没有用

sed -i 's/^fixed-address 135.250.187.128;\nfilename "vxrom.pxe.mg.latest";$/fixed-address 135.250.187.128;\nfilename "vxrom.pxe.mg/' dhcpd.txt 

Input:输入:

host sim127  {hardware ethernet 00:25:90:78:32:B4; fixed-address 135.250.187.127; filename "vxrom.pxe.mg.latest"; next-server 135.250.186.9; }
host sim128  {hardware ethernet 00:25:90:78:2E:FC; fixed-address 135.250.187.128; filename "vxrom.pxe.mg.latest"; next-server 135.250.186.9; }

Output:输出:

host sim127  {hardware ethernet 00:25:90:78:32:B4; fixed-address 135.250.187.127; filename "vxrom.pxe.mg.8_0"; next-server 135.250.186.9; }
host sim128  {hardware ethernet 00:25:90:78:2E:FC; fixed-address 135.250.187.128; filename "vxrom.pxe.mg.latest"; next-server 135.250.186.9; }

sed operates on lines. sed运行。 But your expression includes the newline character \\n .但是您的表达式包含换行符\\n So you have to make sed use more than one line and aborting the evaluation at the newline, like described here .因此,您必须使sed使用多于一行并在换行符处中止评估,如此处所述 So try using the prefix所以尝试使用前缀

:a;N;$!ba; :a;N;$!ba;

in your sed expression before the 's/.../.../g' .'s/.../.../g'之前的sed表达式中。 I also replaced the .我也更换了. with its escaped sequence \\.用它的转义序列\\. and appended a g so that all occurrences would be replaced.并附加一个g以便替换所有出现的事件。 The result would look like this:结果如下所示:

sed -i ':a;N;$!ba;s/fixed-address\n135.250.187.128;\sfilename\s"vxrom.pxe.mg.latest";/fixed-address 135.250.187.128;\ filename "vxrom.pxe.mg";/g' dhcpd.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM