We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Linux find and replace for lines containing specific text

ulink
ulink asked
on
Medium Priority
654 Views
Last Modified: 2008-01-09
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
Comment
Watch Question

Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
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;' *

Author

Commented:
Worked beautifully, thanks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.