tags:

views:

131

answers:

4

Hi,

I need to replace the serials in my zone files, and I thought the easiest would be to use sed to change this. I have the following line to test.

@ IN SOA    ns1.domain.com. webdev.domain.com. (
     2006080401 ; serial
     8H  ; refresh
     2H  ; retry
     1W  ; expire
     4h)  ; minimum ttl

     NS ns1.domain.com.
     NS ns2.domain.com.
     MX 10 mail1.domain.com.
     MX 20 mail2.domain.com.

domain.com.      A   255.255.255.255
mail1     A 255.255.255.255
mail2     A 10.10.10.10

www   CNAME   domain.com.
ftp   CNAME www
webmail  CNAME www

The regular expression I've created using http://rubular.com/ is the following. On rebular it the regex I got matches only one line.

\s*[0-9]\s;\s*serial

So in sed I would use this as follows.

sed -i 's/\s*[0-9]\s;\s*serial/20091218 ; serial/g' *.zone

My problem is that this doesn't change anything in the file. I've tried several things already. Thx for your help

+1  A: 

It looks like you need an asterisk after the second "\s"

sed -i 's/\s*[0-9]\s*;\s*serial/20091218 ; serial/' *.zone

And it might not hurt to put a couple more things in there as well:

sed -i 's/^\s*[0-9]\s*;\s*serial\s$/20091218 ; serial/' *.zone

I took out the "g" since you probably won't have multiple occurrences on one line.

Dennis Williamson
found my problem, actually it was just an old version of sed and egrep I used. I thought regex hadn't changed that much since 2003 :)Thx for your answers though.
latz
A: 

If you don't care about preserving the leading spaces you can simply do:

awk '/serial/{$1=20091218}1'

Result1

@ IN SOA    ns1.domain.com.     webdev.domain.com. (
20091218 ; serial
        8H              ; refresh

On the other hand if getting the serial to line up is important to you, you can do:

awk '/serial/{printf "%8s%-16s; %s\n"," ",20091218,$3;next}1'

Result2

@ IN SOA    ns1.domain.com.     webdev.domain.com. (
        20091218        ; serial
        8H              ; refresh

This will line up perfectly as long as you keep the serial# less than 16 digits.

SiegeX
A: 

Your sed pattern looks for exactly one digit. This works for me:

sed -i 's/\s*[0-9]\+\s;\s*serial/20091218 ; serial/g' *.zone && cat *.zone
fgm
A: 
awk -F"," '/serial$/{$0="20091218 ; serial"}1' OFS="," file