Home > Software engineering >  Perl replace first match after match
Perl replace first match after match

Time:04-09

I have this file:

host test-focal1.mydomain.local {
  hardware ethernet 00:00:00:00:00:00;
  fixed-address 192.168.10.10001;
  server-name "192.168.10.100";
  next-server 192.168.10.100;
  filename "pxelinux.0";
  option routers 192.168.10.1;
  option domain-name "mydomain.local";
  option domain-name-servers 192.168.10.100;
  option domain-search "mydomain.local";
  option host-name "test-focal1";
  option subnet-mask 255.255.255.0;
  option root-path "/fai/nfsroot/focal,rsize=1048576,wsize=8192,acregmin=1800,acregmax=1800,acdirmin=1800,acdirmax=1800,vers=3,mountport=614,port=2049,proto=tcp,mountproto=tcp,mountvers=3,nolock,noacl";
  option option-170 "nfs://192.168.10.100/fai/config-focal";
  site-option-space "PXEL";
  option PXEL.magic xx:xx:xx:xx;
  option PXEL.configfile "pxelinux.cfg/fai-focal";
}

host test-jessie1.mydomain.local {
  hardware ethernet 00:50:56:bd:e7:78;
  fixed-address 192.168.10.10002;
  server-name "192.168.10.100";
  next-server 192.168.10.100;
  filename "pxelinux.0";
  option routers 192.168.10.1;
  option domain-name "mydomain.local";
  option domain-name-servers 192.168.10.100;
  option domain-search "mydomain.local";
  option host-name "test-jessie1";
  option subnet-mask 255.255.255.0;
  option root-path "/fai/nfsroot/focal,rsize=1048576,wsize=8192,acregmin=1800,acregmax=1800,acdirmin=1800,acdirmax=1800,vers=3,mountport=614,port=2049,proto=tcp,mountproto=tcp,mountvers=3,nolock,noacl";
  option option-170 "nfs://192.168.10.100/fai/config-focal";
  site-option-space "PXEL";
  option PXEL.magic xx:xx:xx:xx;
  option PXEL.configfile "pxelinux.cfg/fai-focal";
}

host test-wheezy1.mydomain.local {
  hardware ethernet 00:50:56:bd:e7:79;
  fixed-address 192.168.10.10003;
  server-name "192.168.10.100";
  next-server 192.168.10.100;
  filename "pxelinux.0";
  option routers 192.168.10.1;
  option domain-name "mydomain.local";
  option domain-name-servers 192.168.10.100;
  option domain-search "mydomain.local";
  option host-name "test-wheezy1";
  option subnet-mask 255.255.255.0;
  option root-path "/fai/nfsroot/focal,rsize=1048576,wsize=8192,acregmin=1800,acregmax=1800,acdirmin=1800,acdirmax=1800,vers=3,mountport=614,port=2049,proto=tcp,mountproto=tcp,mountvers=3,nolock,noacl";
  option option-170 "nfs://192.168.10.100/fai/config-focal";
  site-option-space "PXEL";
  option PXEL.magic xx:xx:xx:xx;
  option PXEL.configfile "pxelinux.cfg/fai-focal";
}

I need to replace by perl script PXEL.configfile option value only for a specific host, for example for the host test-focal1.mydomain.local I need to set:

host test-focal1.mydomain.local {
  hardware ethernet 00:00:00:00:00:00;
  fixed-address 192.168.10.10001;
  server-name "192.168.10.100";
  next-server 192.168.10.100;
  filename "pxelinux.0";
  option routers 192.168.10.1;
  option domain-name "mydomain.local";
  option domain-name-servers 192.168.10.100;
  option domain-search "mydomain.local";
  option host-name "test-focal1";
  option subnet-mask 255.255.255.0;
  option root-path "/fai/nfsroot/focal,rsize=1048576,wsize=8192,acregmin=1800,acregmax=1800,acdirmin=1800,acdirmax=1800,vers=3,mountport=614,port=2049,proto=tcp,mountproto=tcp,mountvers=3,nolock,noacl";
  option option-170 "nfs://192.168.10.100/fai/config-focal";
  site-option-space "PXEL";
  option PXEL.magic xx:xx:xx:xx;
  option PXEL.configfile "pxelinux.cfg/test-focal";
}

I tried this code but doesn't work:

`sed -i '/host test-focal1/,/./ s/fai-focal/test-focal1/1' dhcpd.conf`;

What is the correct command? Thanks

CodePudding user response:

How about

perl -pi -e 's/fai-focal/test-focal1/ if /^host test-focal/ .. /^}/' infile

Using the flip-flop operator.

CodePudding user response:

This might work for you (GNU sed):

sed -E '/host test-focal/{:a;N;/}$/!ba;s/fai(-focal)/test\1/}' file

Gather up lines between a line containing host test-focal and a line ending }, then replace fai-focal by test-focal.

CodePudding user response:

I solved with the command below:

`sed -i '/host $fqdn/,/fai-focal/ s/fai-focal/$fqdn/1' dhcpd.conf`;
  • Related