Search and replace text in multiple files.

I had a bunch of html files where a particular style sheet inclusion had to be removed from about 500 odd html files.

Knew this could be done through an sed script , but found a better one using perl from here.

export OLD_TEXT=’<link rel=”stylesheet” type=”text/css” href=”style.css”>’

export NEW_TEXT=’some new text’

and use it in the perl command as follows.

perl -w -i -p -e “s/$OLD_TEXT/$NEW_TEXT/g” *.html

or use it against all .html files from the current directory using find and xargs

find . -name “*.html” | xargs -i perl -w -i -p -e “s/$OLD_TEXT/$NEW_TEXT/g” {}

Add to FacebookAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Yahoo BuzzAdd to Newsvine

About Ajo Paul

A Monk who's yet buy his Ferrari!
This entry was posted in linux and tagged , . Bookmark the permalink.

One Response to Search and replace text in multiple files.

  1. Mihir Patel says:

    Hello,

    It’s nice tutorial. It’s good to share from you. It will help a lot to search text from multiple files.

    Thanks for sharing it….

    Regards,
    Mihir
    ask4itsolutions.com

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre user="" computer="" escaped="">