Liam Delahunty: Home Tips Web Contact
Recommended laptop
under £500
.

Think I deserve a present? See my Amazon Wish List

How to do a search and replace over multiple files

You could also use find and sed, but I find that this little line of perl works nicely.

perl -pi -w -e 's/search/replace/g;' *.php
-e means execute the following line of code.
-i means edit in-place
-w write warnings
-p loop

Example I had the following style sheet in a section:

<link rel="stylesheet" type="text/css" href="../includes/style.css">

and I wanted the following instead:

<link rel="stylesheet" type="text/css" href="admin.css">

As each expression is a regular expression you've got to escape the special characters such as forward slash and .

\.\.\/includes\/style\.css

So the final line of code ends up as

perl -pi -w -e 's/\.\.\/includes\/style\.css/admin\.css/g;' *.php
 

Or a more useful example of adding in an include to the head section:

perl -pi -w -e 's/<\/HEAD>/<?php include($_SERVER[DOCUMENT_ROOT]."\/includes\/tips_top.php");?>\n<\/HEAD>/g;' *.php

Share this!  



Linux Rename Files | Linux Search And Replace Multiple Files | Linux Send Html Email From Command Line


 

  Tips | Home | Search | Contact

Link here: