Suppose there is a situation that a server has multiple files that are nearly identical and you want to remove content from them. In this example suppose that server has multiple NIC cards. There is content that needs to be deleted.
The situation, you have five NIC cards. Each ifcfg-ethx config file contains the values for NM_CONTROLLED, HWADDR, GATEWAY, and DNS1 that you want removed. You could use vim
or another editor to open each file and cut the lines out, then re-save them, or use sed
.
One approach is this 20 line solution. In a script, not a bad option.
sed -i '/NM_CONTROLLED/d' /etc/sysconfig/network-scripts/ifcfg-eth1 sed -i '/NM_CONTROLLED/d' /etc/sysconfig/network-scripts/ifcfg-eth2 sed -i '/NM_CONTROLLED/d' /etc/sysconfig/network-scripts/ifcfg-eth3 sed -i '/NM_CONTROLLED/d' /etc/sysconfig/network-scripts/ifcfg-eth4 sed -i '/NM_CONTROLLED/d' /etc/sysconfig/network-scripts/ifcfg-eth5 sed -i '/HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth1 sed -i '/HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth2 sed -i '/HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth3 sed -i '/HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth4 sed -i '/HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth5 sed -i '/GATEWAY/d' /etc/sysconfig/network-scripts/ifcfg-eth1 sed -i '/GATEWAY/d' /etc/sysconfig/network-scripts/ifcfg-eth2 sed -i '/GATEWAY/d' /etc/sysconfig/network-scripts/ifcfg-eth3 sed -i '/GATEWAY/d' /etc/sysconfig/network-scripts/ifcfg-eth4 sed -i '/GATEWAY/d' /etc/sysconfig/network-scripts/ifcfg-eth5 sed -i '/DNS1/d' /etc/sysconfig/network-scripts/ifcfg-eth1 sed -i '/DNS1/d' /etc/sysconfig/network-scripts/ifcfg-eth2 sed -i '/DNS1/d' /etc/sysconfig/network-scripts/ifcfg-eth3 sed -i '/DNS1/d' /etc/sysconfig/network-scripts/ifcfg-eth4 sed -i '/DNS1/d' /etc/sysconfig/network-scripts/ifcfg-eth5
An alternate approach is this 5 line solution. I am not sure where I found the hint for this online, but this works great too.
sed -i -e '/NM_CONTROLLED/d' -e '/HWADDR/d' -e '/GATEWAY/d' -e '/DNS1/d' /etc/sysconfig/network-scripts/ifcfg-eth1 sed -i -e '/NM_CONTROLLED/d' -e '/HWADDR/d' -e '/GATEWAY/d' -e '/DNS1/d' /etc/sysconfig/network-scripts/ifcfg-eth2 sed -i -e '/NM_CONTROLLED/d' -e '/HWADDR/d' -e '/GATEWAY/d' -e '/DNS1/d' /etc/sysconfig/network-scripts/ifcfg-eth3 sed -i -e '/NM_CONTROLLED/d' -e '/HWADDR/d' -e '/GATEWAY/d' -e '/DNS1/d' /etc/sysconfig/network-scripts/ifcfg-eth4 sed -i -e '/NM_CONTROLLED/d' -e '/HWADDR/d' -e '/GATEWAY/d' -e '/DNS1/d' /etc/sysconfig/network-scripts/ifcfg-eth5
A cleaner 5 line solution.
sed -i '/NM_CONTROLLED/d;/HWADDR/d;/GATEWAY/d;/DNS1/d' /etc/sysconfig/network-scripts/ifcfg-eth1 sed -i '/NM_CONTROLLED/d;/HWADDR/d;/GATEWAY/d;/DNS1/d' /etc/sysconfig/network-scripts/ifcfg-eth2 sed -i '/NM_CONTROLLED/d;/HWADDR/d;/GATEWAY/d;/DNS1/d' /etc/sysconfig/network-scripts/ifcfg-eth3 sed -i '/NM_CONTROLLED/d;/HWADDR/d;/GATEWAY/d;/DNS1/d' /etc/sysconfig/network-scripts/ifcfg-eth4 sed -i '/NM_CONTROLLED/d;/HWADDR/d;/GATEWAY/d;/DNS1/d' /etc/sysconfig/network-scripts/ifcfg-eth5
Then, I thought about this one line solution. I haven’t fully tested it, but tested it enough to post it here.
sed -i '/NM_CONTROLLED/d;/HWADDR/d;/GATEWAY/d;/DNS1/d' /etc/sysconfig/network-scripts/ifcfg-eth{1..5}
Source(s)
http://stackoverflow.com/questions/26568952/how-to-replace-multiple-patterns-at-once-with-sed