LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-06-2011, 01:41 PM   #1
rhbegin
Member
 
Registered: Oct 2003
Location: Arkansas, NWA
Distribution: Fedora/CentOS/SL6
Posts: 381

Rep: Reputation: 23
Unhappy How to use :%s to replace octet in IP range


I am having to add some new zone files in DNS - Bind.

Example my file contains:

dslnt2-a1-c5-g5 in a 10.201.1.5
dslnt3-a2-c5-g6 in a 10.201.1.6

I in vi editor mode trying to change the IP address only to
dslnt2-a1-c5-g5 in a 10.201.0.5
dslnt2-a2-c5-g6 in a 10.201.0.6

I need to change the 3 octet from a (.1) to a (.0)

I am trying the following:

:%s/\.1/\/.0/g

But this results in 10.201/.0.5

Any hints on how to change the 3rd octet without the '/' being added in???

Also, it adds a 0 to the 4th octet making it 019 020 ect???

Any help would be great, I just need to change only the 3rd octet from a 1 to a 0 without
changing the IP otherwise.

Would there be another way to change the 3rd octet that is easier?

any advice/help would be great

Last edited by rhbegin; 04-06-2011 at 01:55 PM. Reason: another method than vi
 
Old 04-06-2011, 02:05 PM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 12,234

Rep: Reputation: 1685Reputation: 1685Reputation: 1685Reputation: 1685Reputation: 1685Reputation: 1685Reputation: 1685Reputation: 1685Reputation: 1685Reputation: 1685Reputation: 1685
Hi,

You can use sed:
Code:
sed -i 's/10.201.1/10.201.0/g' zonefile
Regards
 
Old 04-06-2011, 02:26 PM   #3
rhbegin
Member
 
Registered: Oct 2003
Location: Arkansas, NWA
Distribution: Fedora/CentOS/SL6
Posts: 381

Original Poster
Rep: Reputation: 23
Quote:
Originally Posted by bathory View Post
Hi,

You can use sed:
Code:
sed -i 's/10.201.1/10.201.0/g' zonefile
Regards
Thanks a million!

My manager came by and wrote this down just a minute ago:

:%s/10\.210\.1/10\.210\.0/

I always seem to get stuck when it comes to having to change zone files (I mean changing octets), by the looks of it I got a lot to change.

Thanks again for the quick reply!


Last edited by rhbegin; 04-06-2011 at 02:33 PM. Reason: added comment
 
Old 04-06-2011, 02:28 PM   #4
rhbegin
Member
 
Registered: Oct 2003
Location: Arkansas, NWA
Distribution: Fedora/CentOS/SL6
Posts: 381

Original Poster
Rep: Reputation: 23
My manager walked by casually and off the top of his head wrote down:


:%s/10\.210\.1/10\.210\.0/

I had the exact problem today, I am having to edit a lot of zone files and add in new ones for fiber...

As always LinuxQuestions rules!

 
Old 04-06-2011, 02:50 PM   #5
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 682

Rep: Reputation: 105Reputation: 105
a bit more global, but what would've worked as well:

:%s/\.1\./\.0\./g

(replace all (literal dot)1(literal dot) to (literal dot)0(literal dot))

\. = literal dot

so you get .1. becomes .0.

--

Now you have to change many of bind's files, so what you can do is run below code from the directory the zone files are:
Code:
OLDZONE="10.201.1."
NEWZONE="10.201.0."
grep -l "${OLDZONE}" * | while read filename
do
CURTIME=`date +"%Y%m%d%H%M%S"`
cp -v ${filename} /tmp/${filename}.${CURTIME}  # backup to /tmp 
sed -i "s/${OLDZONE}/${NEWZONE}/g" ${filename} # inline sed
done
 
Old 04-06-2011, 08:00 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,611

Rep: Reputation: 2937Reputation: 2937Reputation: 2937Reputation: 2937Reputation: 2937Reputation: 2937Reputation: 2937Reputation: 2937Reputation: 2937Reputation: 2937Reputation: 2937
Assuming the items appear at the end as per your example, you could do:
Code:
sed -r -i 's/(^.*\.)[^.]+(\..*)$/\10\2/' zonefile
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How can I replace a certain range of characters in linux/unix? btacuso Programming 6 03-11-2010 09:38 AM
[SOLVED] What are short range link and long range links in routing? mq15 Linux - Networking 6 06-26-2009 11:16 PM
Sed - replace text only in a certain range twantrd Programming 4 11-30-2006 06:54 PM
application/octet-stream aaru_ali Mandriva 2 02-01-2005 02:47 PM
Application / octet stream Daredevil Programming 0 04-02-2004 09:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration