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

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

LVL 2
Shamsul KamalJunior TechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TintinCommented:

#!/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
Shamsul KamalJunior TechAuthor Commented:
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
TintinCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Shamsul KamalJunior TechAuthor Commented:
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
TintinCommented:
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
Shamsul KamalJunior TechAuthor Commented:
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
TintinCommented:
Sorry, my mistake

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

should be

for db in $(find /var/named -name "*.db")
0
Shamsul KamalJunior TechAuthor Commented:
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
TintinCommented:
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
Shamsul KamalJunior TechAuthor Commented:
Thanks for helping
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.