john wilson john wilson - 4 months ago 16
Bash Question

Replace patterns on file except one line in it

I am looking for a solution to replace one pattern on a file except one line in it.

For example:
zone file:

> cat /var/named/test.com.db
; cPanel first:11.11.0-BETA_16994 (update_time):1468656855 Cpanel::ZoneFile::VERSION:1.3 hostname:server.test.com latest:11.56.0.13
; Zone file for test.com
$TTL 14400
test.com. 86400 IN SOA ns1.test.com. cpanel.test.com. (
2016071602 ;Serial Number
14400 ;refresh
7200 ;retry
2419200 ;expire
43200 )

test.com. 14400 IN A 192.168.1.100

localhost 14400 IN A 127.0.0.1

test.com. 14400 IN MX 0 test.com.

mail 14400 IN CNAME test.com.
www 14400 IN CNAME test.com.
ftp 14400 IN CNAME test.com.
webdisk 14400 IN A 192.168.1.100
cpcalendars 14400 IN A 192.168.1.100
test.com. IN TXT "v=spf1 +a +mx +ip4:192.168.1.100 ~all"


I want to replace all current IP address 192.168.1.100 in it with 192.168.1.101 except the "spf" record line.

How can this be done?.

Answer
sed '/spf/! s/192\.168\.1\.100/192.168.1.101/'

On lines which do not match /spf/ do the requested substitution. The backslashes prevent the . from matching any character, of course. If you have any lines where the IP address could appear multiple times, you could add a g at the end. If you think it is a real problem, you could worry about matching 9192.168.1.1001 (that is, you if you think the IP address might appear as a substring of something that isn't an IP address).

Comments