LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   using sed to change zone file serial number (http://www.linuxquestions.org/questions/programming-9/using-sed-to-change-zone-file-serial-number-919574/)

countrydj 12-19-2011 06:43 PM

using sed to change zone file serial number
 
Hi Guys...

I have just started to learn about 'sed' and find it very useful.

I wonder if it is possible to change the serial number in DNS zone files enblock.

eg. Change: 2010121901()or whatever the number is) To: 2011121901

In perl there is a command:
Code:

perl -i -p -e 's/\d\d\d\d\d\d\d\d\d\d/2011121901/' *
This will run through all the zone files in the directory and change them all.

Has anybody any ideas ???

John C

Cedrik 12-19-2011 06:59 PM

The perl command could be shortened to:
Code:

perl -i -pe 's/\d{10}/2011121901/' *
A similar sed line:
Code:

sed -i -e 's/[0-9]\{10\}/2011121901/' *
edit:
Both commands search for 10 digits numbers, maybe you could restrict the research more

Tinkster 12-19-2011 07:13 PM

sed or not, I find that approach somewhat risque ... while it
doesn't make too much sense there's still nothing to stop a user
from defining hostnames that consist of 10 digits - I don't
think you'd want ALL of those to be equal to your new serial?


That said:
Code:

sed -r -e '/^[\t ]+[0-9]{10}[\t ]*;[\t ]*serial/ s/[0-9]{10}/2011121901/' *
I made the word "serial" a necessary condition for the replacement,
just for sanitys sake. And of course bear in mind that if you change
the file for the 2nd time on the same day the tailing two digits need
to be incremented ... thought I'd chuck that in for good measure. ;}



Cheers,
Tink


P.S.: Beat too it :} ... that's what happens if you start responding
and walk away. =o)

countrydj 12-19-2011 07:13 PM

Hi Cedrik..

Thank you so much for your extremely quick reply.

I have tested both these commands and found that they work fine for me.
This is going to save me a lot of time when I transfer all my domains to a new server.

THANK YOU.

John C

countrydj 12-19-2011 07:22 PM

Hi Tinkster...
Quote:

Btw, I'll rather attempt to aid you in helping yourself than
giving you a "turn-key solution". I believe in enabling people
to help themselves, rather than carrying them everywhere. ;}
I have been looking for around 4 hous for an answer to my question.
I tried man pages and google and didn't get anywhere near an answer.
That was when I decided to put a post on the forum.
I can only spend so much time looking and getting nowhere

Tinkster 12-19-2011 07:41 PM

Quote:

Originally Posted by countrydj (Post 4554075)
Hi Tinkster...

I have been looking for around 4 hous for an answer to my question.
I tried man pages and google and didn't get anywhere near an answer.
That was when I decided to put a post on the forum.
I can only spend so much time looking and getting nowhere


Whether or not you chose to take my SIGNATURE personally is
entirely up to you my friend ... =o)


Cheers,
Tink

sundialsvcs 12-22-2011 06:09 AM

Now, if it were me, I would as a matter of principle always write such code to read one file as input and to write another copy of that file as output ... then to do some renaming so that when the program finished it had the "new" copy in-place and one or more "backup" copies nearby.

:eek:
Code:

set_my_speed( miles_per_hour(100) );
shoot(foot);

:eek:


All times are GMT -5. The time now is 04:30 PM.