• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 641
  • 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
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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