Search and replace text in multiple files. 1

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

One comment on “Search and replace text in multiple files.

  1. Reply Mihir Patel Sep 23,2009 5:10 pm


    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….


Leave a Reply