john wilson john wilson -5 years ago 168
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/
; cPanel first:11.11.0-BETA_16994 (update_time):1468656855 Cpanel::ZoneFile::VERSION:1.3 latest:
; Zone file for
$TTL 14400 86400 IN SOA (
2016071602 ;Serial Number
14400 ;refresh
7200 ;retry
2419200 ;expire
43200 ) 14400 IN A

localhost 14400 IN A 14400 IN MX 0

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

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

How can this be done?.

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

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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download