Solved

Would like to search and replace specific string in file in one directory (linux)

Posted on 2008-10-28
10
289 Views
Last Modified: 2013-12-16
Hi,

I would like to request an assistant.

May i know how to create a script to search and replace a few string in a file ?

Attached is the dns zone file in which i would like to do the following :

1. search all files in a directory ended with ".db" and replace all string "600" to "14400"
2. After that replacing the specific string below :

From :

                600             ; refresh, seconds
                600             ; retry, seconds
                600             ; expire, seconds
                600 )           ; minimum, seconds

to :

      86400            ; refresh, seconds
      7200            ; retry, seconds
      3600000            ; expire, seconds
      86400 )            ; minimum, seconds


The location of the files is in "/var/named"
The filename will end with ".db" eg: domainname.com.db"

Appreciates if anybody can help me on this.


P/S : the reason i need to replace the "600" in my STEP 1 is to replace the string that are not specific above.


Thank you,

Regards,
Sham
; cPanel 11.18.1-CURRENT_21558
; Zone file for domainname.com
$TTL 600
@      600      IN      SOA     dns60.hostingcompany.com. report.hostingcompany.com. (
                2008041401      ; serial, todays date+todays
                600             ; refresh, seconds
                600             ; retry, seconds
                600             ; expire, seconds
                600 )           ; minimum, seconds
 
domainname.com. 600 IN NS dns60.hostingcompany.com.
domainname.com. 600 IN NS dns61.hostingcompany.com.
 
domainname.com. IN A 124.217.243.160
 
localhost.domainnamecom. IN A 127.0.0.1
 
domainname.com. IN MX 0 domainname.com.
 
mail IN CNAME domainname.com.
www IN CNAME domainname.com.
ftp IN CNAME domainname.com.

Open in new window

0
Comment
Question by:smksa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 22828670

#!/bin/bash
for db in $(find /var/named -name "*.db)
do
  sed -i.bak -e "s/600.*refresh, seconds/86400    ;refresh, seconds/" \
    -e "s/600.*retry, seconds/7200      ;retry, seconds/" \
    -e "s/600.*expire, seconds/3600000  ;expire, seconds/" \
    -e "s/600.*minimum, seconds/86400   ;expire, seconds/" $db
done

Open in new window

0
 
LVL 2

Author Comment

by:smksa
ID: 22828688
Hi,

Thanks TinTin,

But how about my Step 1 in which i would like all string of 600 to be changed to 14400 first before the specific string as you given above.

Thank you,

Regards,
Sham
0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 22828721
What's the purpose of Step 1?  Do you want to end up with

@TTL 14440
domainname.com. 14400 IN NS dns60.hostingcompany.com.
domainname.com. 14400 IN NS dns61.hostingcompany.com.

if so, then change the script to



#!/bin/bash
for db in $(find /var/named -name "*.db)
do
  sed -i.bak -e "s/600/14400/" \
    -e "s/14400.*refresh, seconds/86400    ;refresh, seconds/" \
    -e "s/14400.*retry, seconds/7200      ;retry, seconds/" \
    -e "s/14400.*expire, seconds/3600000  ;expire, seconds/" \
    -e "s/14400.*minimum, seconds/86400   ;expire, seconds/" $db
done

Open in new window

0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 2

Author Comment

by:smksa
ID: 22828749
Hi TinTin,

Thanks ...

But i received the following error message :

root@svr8 [/]# ./changettl.sh
./changettl.sh: line 8: unexpected EOF while looking for matching `"'
./changettl.sh: line 10: syntax error: unexpected end of file
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22828763
if you got those errors, you must have copy/pasted the script I gave you incorrectly.

Please check again, or copy/paste the version you are using here.
0
 
LVL 2

Author Comment

by:smksa
ID: 22828777
Hi,

Attached is the exact copy of the script ...

Can you help to check where went wrong ?

Thanks..
#!/bin/bash
for db in $(find /var/named -name "*.db)
do
  sed -i.bak -e "s/600/14400/" \
    -e "s/14400.*refresh, seconds/86400    ;refresh, seconds/" \
    -e "s/14400.*retry, seconds/7200      ;retry, seconds/" \
    -e "s/14400.*expire, seconds/3600000  ;expire, seconds/" \
    -e "s/14400.*minimum, seconds/86400   ;expire, seconds/" $db
done

Open in new window

0
 
LVL 48

Expert Comment

by:Tintin
ID: 22828834
Sorry, my mistake

for db in $(find /var/named -name "*.db)

should be

for db in $(find /var/named -name "*.db")
0
 
LVL 2

Author Comment

by:smksa
ID: 22828900
Hi,

It seems there are a mistake in the scripts...

FYI, the last part "86400   ;expire, seconds" should be "86400   ) ;minimum, seconds"

you missed the ")" ,

I have runned the script but not yet restart the dns ...

May i know how to move all .db.bak file back to .db ?

Thank you.

Regards,
Sham.



0
 
LVL 48

Expert Comment

by:Tintin
ID: 22828973
To revert all .bak files, do


for db in $(find /var/named -name "*.bak")
do
  mv $db ${db%%.bak}
done

Open in new window

0
 
LVL 2

Author Closing Comment

by:smksa
ID: 31511054
Thanks for helping
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question