inside the mind of a linux admin

search and replace


Example: Customer has been a victim of iframe code injections in multiple pages. Use this grep/sed command will come in handy for clearing the majority of them out. Please be careful and test first with just the grep command to ensure you don’t clear out anything you need.
Does NOT backup files, be sure to backup first!

download: http://erikimh.com/code/rmcodeinjections.sh

this will remove all iframe injections from a particular file or wildcard set of files

usage example: ./rmcodeinjections.sh *.php


Example: Replacing /var/named domains IP addresses with perl

Sometimes you need to replace an instance of one string in a file with another string. If there’s multiple occurrences, this can be a pain. Here’s a simple example of how you can use perl to replace all instances of an IP address for a moved a domain.

To do it:

perl -i.bak -pe “s/0.0.0.0/1.2.3.4/g” /var/named/domain.com.db

This example shows how to change from IP 0.0.0.0 to 1.2.3.4
This will also makes a backup of /var/named/domain.com.db as /var/named/domain.com.db.bkp

If you’re feeling ballsy and do not want backup run:

perl -i -pe “s/0.0.0.0/1.2.3.4/g” /var/named/domain.com.db

One alternate way would be (without perl):

replace 0.0.0.0 1.2.3.4 — /var/named/domain.com.db

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.