sed – match and replace BIND/DNS serial number programmatically

This post is over 3 years old, so please keep in mind that some of its content might not be relevant anymore.

I had to make some modifications to several BIND zone files, sed proved itself invaluable once again!

All the zone files looked something like this:

domain.com. IN SOA ns1.domain.com. postmaster.domain.com. (
               2015021001   ; Serial
               600          ; Refresh (10 minutes) 
               3600         ; Retry (1h) 
               604800       ; Expire (7d) 
               3300 )       ; TTL (1h)
domain.com.    IN NS ns1.domain.com.
domain.com.    IN NS ns2.domain.com.
[..]

After having made all the correct replacements, I needed to match the serial number of each zone and replace it with today’s date.

The following sed command, searches for a 10 digit string across all files, and replaces it with the one I have hard-coded.

sed -i 's/[0-9]\{10\}/2017012701/' *

If you are worried about this matching any other 10 digit strings in your zone files, you can limit sed‘s range by restricting the research to those lines which contain the word “Serial”, like so:

sed -i '/Serial/ s/[0-9]\{10\}/2017012701/' *

Hope it helps!
Andrea

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: