Replace serial number in zone files with sed

I need to replace the series in the zone files, and I thought it was easiest to use sed to change this. I have the following line for testing.

@ 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

, http://rubular.com/, . rebular , , .

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

, sed .

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

, . .

+3
5

, "\ s"

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

:

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

"g", , , .

+2

, :

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

Result1

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

, , :

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

2

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

, 16 .

+1

Here is the perl method that worked well for me:

cat /var/named/zonefile.db | perl -e "while(<>){ s/\d+(\s*;\s*[sS]erial)/20091218\1/; print; }"

Keep leading spaces.

+1
source

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

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

Source: https://habr.com/ru/post/1725936/