sed运用实例一——基于变量的动态替换-创新互联
在工作中我需要修改两个文件:
我们提供的服务有:成都网站建设、网站制作、微信公众号开发、网站优化、网站认证、西安ssl等。为上1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的西安网站制作公司文件一:/etc/vmware/networking
- VERSION=1,0
- answer VNET_1_DHCP yes
- answer VNET_1_DHCP_CFG_HASH 50CE8453B1072EA401BFFA704E6C01F7AE0BA67C
- answer VNET_1_HOSTONLY_NETMASK 255.255.255.0
- answer VNET_1_HOSTONLY_SUBNET 172.16.77.0
- answer VNET_1_VIRTUAL_ADAPTER yes
- answer VNET_8_DHCP yes
- answer VNET_8_DHCP_CFG_HASH 1DA1A936DCB3AA3313CF293A9C96D25B76AB48A2
- answer VNET_8_HOSTONLY_NETMASK 255.255.255.0
- answer VNET_8_HOSTONLY_SUBNET 192.168.43.0
- answer VNET_8_NAT yes
- answer VNET_8_VIRTUAL_ADAPTER yes
文件二:/etc/vmware/vmnet8/dhcpd/dhcpd.conf
- # Configuration file for ISC 2.0 vmnet-dhcpd operating on vmnet8.
- #
- # This file was automatically generated by the VMware configuration program.
- # See Instructions below if you want to modify it.
- #
- # We set domain-name-servers to make some DHCP clients happy
- # (dhclient as configured in SuSE, TurboLinux, etc.).
- # We also supply a domain name to make pump (Red Hat 6.x) happy.
- #
- ###### VMNET DHCP Configuration. Start of "DO NOT MODIFY SECTION" #####
- # Modification Instructions: This section of the configuration file contains
- # information generated by the configuration program. Do not modify this
- # section.
- # You are free to modify everything else. Also, this section must start
- # on a new line
- # This file will get backed up with a different name in the same directory
- # if this section is edited and you try to configure DHCP again.
- # Written at: 03/22/2013 15:49:55
- allow unknown-clients;
- default-lease-time 1800; # default is 30 minutes
- max-lease-time 7200; # default is 2 hours
- subnet 192.168.43.0 netmask 255.255.255.0 {
- range 192.168.43.128 192.168.43.254;
- option broadcast-address 192.168.43.255;
- option domain-name-servers 192.168.43.2;
- option domain-name localdomain;
- default-lease-time 1800; # default is 30 minutes
- max-lease-time 7200; # default is 2 hours
- option netbios-name-servers 192.168.43.2;
- option routers 192.168.43.2;
- }
- host vmnet8 {
- hardware ethernet 00:50:56:C0:00:08;
- fixed-address 192.168.43.1;
- option domain-name-servers 0.0.0.0;
- option domain-name "";
- option routers 0.0.0.0;
- }
- ####### VMNET DHCP Configuration. End of "DO NOT MODIFY SECTION" #######
需求:
将两个文件中的"192.168.43"修改为"202.16.22",不过,有时"192.168.43"会表现为其它的IP值,而要修改的目标字串"202.16.22"也可能会随着环境的改变另行设置.
基于此需求,本人编写了如下测试脚本:
针对文件一:
- #!/bin/bash
- # $vmnat_subnet_old变量表示vmnat8原有的子网配置,注意只截取前三段
- vmnat_subnet_old=`grep "VNET_8_HOSTONLY_SUBNET" /etc/vmware/networking | sed 's/..$//' | cut -d' ' -f 3`
- # $vmnat_subnet_new变量表示vmnat8子网的目标配置,注意只截取前三段
- vmnat_subnet_new='192.168.40'
- cp /etc/vmware/networking /etc/vmware/networking_bak0
- sed -i '/VNET_8_HOSTONLY_SUBNET/s/'"$vmnat_subnet_old"'/'"$vmnat_subnet_new"'/' /etc/vmware/networking
- grep "$vmnat_subnet_new" /etc/vmware/networking
- cp /etc/vmware/networking_bak0 /etc/vmware/networking
针对文件二:
- #!/bin/bash
- # $vmnat_subnet_new变量表示vmnat8子网的目标配置,注意只截取前三段
- vmnat_subnet_new='192.168.40'
- # $vmnat_subnet_old变量表示vmnat8原有的子网配置,注意只截取前三段
- vmnat_subnet_old=`grep "VNET_8_HOSTONLY_SUBNET" /etc/vmware/networking | sed 's/..$//' | cut -d' ' -f 3`
- cat /etc/vmware/vmnet8/dhcpd/dhcpd.conf
- cp /etc/vmware/vmnet8/dhcpd/dhcpd.conf /etc/vmware/vmnet8/dhcpd/dhcpd.conf_bak0
- sed -i '/'"$vmnat_subnet_old"'/s//'"$vmnat_subnet_new"'/g' /etc/vmware/vmnet8/dhcpd/dhcpd.conf
- grep "$vmnat_subnet_new" /etc/vmware/vmnet8/dhcpd/dhcpd.conf
- cp /etc/vmware/vmnet8/dhcpd/dhcpd.conf_bak0 /etc/vmware/vmnet8/dhcpd/dhcpd.conf
优点:
可以不用事先知道要替换的字符是(本例中:192.168.43)什么,只需要知道自己想替换为(本例中:202.16.22)什么就可以了。
呵呵,我的工作桌面截图。
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享名称:sed运用实例一——基于变量的动态替换-创新互联
文章起源:http://scyanting.com/article/disssc.html