• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 636
  • Last Modified:

Linux find and replace for lines containing specific text

I need to change MX records across a large amount of domains and I am looking for a script to automate the process. The problem lies in that I need to change the MX record, but not cname records. For example:

From
@          IN          MX          100       mail.somedomain.com
@          IN          MX          100       mail.somedomain.com
mail1      IN          CNAME                mail.somedomain.com
mail2      IN          CNAME                mail.somedomain.com

To
@          IN          MX          100       mail.newdomain.com
@          IN          MX          100       mail.newdomain.com
mail1      IN          CNAME                mail.somedomain.com
mail2      IN          CNAME                mail.somedomain.com

So basically I need something that says go through every file in this folder and look for lines containing MX. On those lines, replace mail.somedomain.com with mail.newdomain.com
0
ulink
Asked:
ulink
1 Solution
 
avizitCommented:
perl -i.bak -lape 's/mail\.somedomain\.com/mail\.newdomain\.com/g if $F[2] eq 'MX''  *

try the above .. if it works fine in your tests you can remove the  .bak after -i
0
 
ozoCommented:
awk '$3=="MX"{sub("mail.somedomain.com","mail.newdomain.com")}{print}'
 < From > To
0
 
shoaibbhatti007Commented:
sed 's/mail.somedomain.com/mail.newdomain.com/' MX_file > tmp_file
mv tmp_file MX_file
0
 
shoaibbhatti007Commented:
perl -pi -w -e 's/mail\.somedomain\.com/mail\.newdomain\.com/g;' *
0
 
ulinkAuthor Commented:
Worked beautifully, thanks
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now