I need to increment named serial and I want to do it with sed. I've done it like this:

echo 2015092315 | gsed s/20[0-9][0-9][0-9][0-9]*/`date +%Y%m%d%H`/g

But then I realized that updates to zone file might happen more than once an hour and this above will not be enough.

Now I want to increment the serial by 1. How to do that? I came up with a:

gsed 's/20[0-9][0-9][0-9][0-9]*/expr & + 1/e'

which does work but it deletes the beginning of line (spaces) and the end of the line (comment), and gives error about a comment (sh: serial: not found) which I do not want.

  • A date call will usually work, except when the system gets its clock set wrong. I've seen servers boot four years in the future for no apparent reason, for example. I've otherwise moved my DNS over to nsupdate related tools, and let the nameserver bump the serial for me. – thrig Sep 23 '15 at 16:33
  • any reason you don't want to use date +%s in your first gsed? – Jeff Schaller Sep 23 '15 at 16:45
  • @thrig: date will not work for me here because I need to update bind after vm deployment from some interface, which could happen more than once an hour. Didn't thougt about Jeff's idea.. – czbg Sep 23 '15 at 17:55
  • @JeffSchaller: Thanks for the idea Jeff. I assume I have to restart named servers if I do it like that, since right now 2015092215 is greater than 1443031008, right? – czbg Sep 23 '15 at 17:57
  • I would think that a normal reload would do the trick -- the slaves only care that the serial number is higher; I just wanted to point out an alternative. For example, you could add %M to the end of your current solution to add minute-level resolution. – Jeff Schaller Sep 23 '15 at 18:07

Perl to the rescue:

perl -pe 's/(20[0-9]{3,})/$1+1/e' file
  • Thanks choroba. I was aware of perl's possibility, but I wanted to do it with sed. So, it isn't possible to achive this part: "$1+1" with sed? I'm thinking now more about Jeff's solution.. – czbg Sep 23 '15 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.