简体   繁体   中英

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

What is the sed command to replace a below line from a file. I was using below sed command to replace a line, But it didn't work

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. But your expression includes the newline character \\n . So you have to make sed use more than one line and aborting the evaluation at the newline, like described here . So try using the prefix

:a;N;$!ba;

in your sed expression before the 's/.../.../g' . I also replaced the . with its escaped sequence \\. and appended a g so that all occurrences would be replaced. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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