简体   繁体   English

用sed替换区域文件中的序列号

[英]Replace serial in zone files with sed

I need to replace the serials in my zone files, and I thought the easiest would be to use sed to change this. 我需要替换区域文件中的序列号,我认为最简单的方法是使用sed来改变它。 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. 我使用http://rubular.com/创建的正则表达式如下。 On rebular it the regex I got matches only one line. 在rebular上,我得到的正则表达式只匹配一行。

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

So in sed I would use this as follows. 所以在sed中我会使用如下。

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 谢谢你的帮助

It looks like you need an asterisk after the second "\\s" 在第二个“\\ 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. 我拿出了“g”,因为你可能不会在一行上出现多次。

If you don't care about preserving the leading spaces you can simply do: 如果您不关心保留领先的空间,您可以简单地做:

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

Result1 结果1

@ 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 结果2

@ 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. 只要保持序列号小于16位,这将完美排列。

Here is a perl method that worked well for me: 这是一个适合我的perl方法:

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

Preserve leading spaces. 保留领先的空间。

Your sed pattern looks for exactly one digit. 您的sed模式只查找一位数。 This works for me: 这对我有用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM