TutorialsRoom.com, Where knowledge lands

Search And Replace Multi-Line Text in Files

search-and-replace-text (1K)
In this tutorial we will see how to find and optionally replace multiline text in multiple files - complete html tags, paragraphs...etc - even if the contents of the searched text are not completely the same! Read this tutorial to learn how to do that.

For example, what if you needed to change complete html div tags with all their contents? I'm sure most of us faced this problem and maybe had to open and change hundreds or maybe thousands of web pages by hand and you know that could be a webmaster nightmare. The problem comes when you need to change multi-line text and not a single line, also because the text may not be completely the same - like having more spaces, empty lines...etc - or even worse, the contents are different! Most tools would miss many if not most of the web pages you needed to change.

Consider this tough task: I have decided to use templates and replace my static site with a dynamic one so for a start, I want to replace all the html divs containing static text with html divs containing php includes or php functions to get the data from my database.

Here is a sample of what I'm talking about, I want to replace this:
<div id="intro">
text line 1
text line 2
........
</div>
with:
<div id="intro">
<?php EchoMyIntro();?>
</div>
Notice that: text line 1, text line 2,... are different from page to page and I want to replace them in all pages, let's say thousands of pages).

In this tutorial, we will use InfoRapid Search & Replace, which is a freeware for private purposes, and we will learn some basics of Regular Expressions, which provides flexible means for identifying strings of text of interest. We will not get deep into the somewhat difficult details of Regular Expressions - or REGEX - but only some basics.You can change some simple words to make this tutorial suitable for your needs.

  1. Open InfoRapid Search & Replace. As you can see, InfoRapid Search & Replace consists of two main parts, the upper part which shows the search results and the lower part where we will type our search criteria and what we want to be replaced with.
    search_replace_01 (12K)
  2. Let's start with the obvious fields, so leave "search for" for now, "In files" means the file types you want to be searched (like *.html) notice that you can use more then one file type seperated by |, "In Directory" is the starting directory where the program will search for files notice that you can check the "With Subdirectories".
  3. In the "Search Method", choose "Pattern matching", this is very important also check the "Multiline seach" and "Shortest match length". Most probably you will follow the first tutorial image and change only the directory to match your directory. Make sure to BACKUP your files in that directory first because you WILL NOT be able to undo your changes after the replacement.
  4. Now, we want to tell the program to search for all the text that starts with <div id="intro" and all the text, spaces and lines that come after it until it reaches the </div> which will end our html div and replace all that with the above sample, it's obvious that we cannot use regular search and replace methods here and here comes the beauty of the regular expressions when you know how to use them!
  5. In the "Search for" field just type this simple line: <div id="intro".*</div> and that's it! The dot (.) in regular expressions means match any single character and the asterisk (*) means zero or more of the preceding element so both of them together mean any characters, many of the programs that use regex for searching would exclude the newline but InfoRapid Search & Replace will include them if you check the "Multiline search".
  6. We are ready now, before actually replace our text let's make sure every thing is right so hit the Start button.
    search_replace_02 (12K)
  7. As you can see in the above image, the program displays a table with your search criteria and the last row shows that it has found 45 pages with matching text out of whole 46 html pages (which is right in my case). The good thing is that you can review all the text that will be replaced before actually replacing anything, just scroll and see, all the red text will be replaced. If you think those were not the results you were expecting - like here, I have a div inside div so I have to replace the inner div first -, No Problem! Click "Show Search Dialog", fix your search criteria and hit Start again until you are satisfied with the results.
  8. MAKE A BACKUP of your work before replacing the text, click the Replace tab and type the new text in the "Replace with" field. In my case I have typed:
    <div id="intro">\n<?php EchoMyIntro();?>\n</div>
  9. The program will replace \n with a new line, if you want to see all the available options just click the >> button on the right of the "Replace with" field.
  10. Click Start button, you will see that the program is asking you to confirm the replacement and showing you what will be replaced in the actual file, you can now just click yes and see your changes one by one or check "Replace All" to let the program make all the replacements for your.
    search_replace_03 (13K)
It took about 2 seconds in my case, that could save you hours of work especially in big sites with thousands of web pages. It's just an example but you can use your imagination to search and replace almost any thing in any text file not necessarily html files.

More Useful Tutorials

Css Basics - the First 5 Steps Towards Mastery
This tutorial covers the first five steps everyone should take to fully understand (and eventually master) CSS.

Create a Cool Animated Navigation With Css And Jquery
Use the magic of javascript to avoid Flash and build a really cool animated navigation menu using just CSS and jQuery.

Side Navigation
Create a rollover image navigation for your website!

Current Comments (4)

Sun, 15 June 2008 09:36am (GMT)

Thanks - i have been looking for such a tutorial or even a css tutorial on you tube simple html - thanks It will make my life easier

reene Said,
Fri, 18 July 2008 12:05pm (GMT)

Thanks for the tutorial. I m trying to delete a particular line of a file. Any help ?

TutorialsRoom Said,
Fri, 18 July 2008 23:27pm (GMT)

Do the same steps and type a space in the replace tab.

jah Said,
Tue, 29 July 2008 15:46pm (GMT)

Thank you so much. This is helping me quite a lot by taking the tediousness out of changing the navigation tree on my pages!


Leave Your Comment (English Only Please)


(Required)


(Optional, will not be shown in public)


(Including http:// - Optional)



Type the sum of 5 + 3 (Required)


Copyright © 2007-2008 Hazem Osman. All rights reserved. Terms & Conditions