1

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; }
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 1
    you tagged the question with the answer: sed. or awk, or perl, or any other text processing language. "unix" doesn't do this thing. utilities that run under unix do. – Marc B Apr 20 '16 at 17:19
  • What exactly do you want to match? It looks like you want to make sure that `filename "vxrom.pxe.mg.latest"` is on the subsequent line after matching fixed-address 135.250.187.128. Try to articulate your requirements in plain english. –  Apr 21 '16 at 01:08

1 Answers1

1

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
Community
  • 1
  • 1
zx485
  • 28,498
  • 28
  • 50
  • 59
  • I have found one more solution here to resolve this issue . First we use grep command to get the line number of searching attribute from file #grep -n "" | cut -d ":" -f1 then we store the output of grep command in an attribute and increment the value of that attribute after that we use that attribute value in a sed command to substitute the value in a file.#sed -i 's//' – hemant belwal Apr 22 '16 at 03:48