Link to home
Start Free TrialLog in
Avatar of ulink
ulink

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of avizit
avizit

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
awk '$3=="MX"{sub("mail.somedomain.com","mail.newdomain.com")}{print}'
 < From > To
sed 's/mail.somedomain.com/mail.newdomain.com/' MX_file > tmp_file
mv tmp_file MX_file
perl -pi -w -e 's/mail\.somedomain\.com/mail\.newdomain\.com/g;' *
Avatar of ulink
ulink

ASKER

Worked beautifully, thanks