Search And Replace Multi-Line Text in Files
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:text line 1
text line 2
........
</div>
<?php EchoMyIntro();?>
</div>
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.
- 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.
- 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".
- 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.
- 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!
- 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".
- We are ready now, before actually replace our text
let's make sure every thing is right so hit the Start button.
- 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.
- 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> - 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.
- 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.
More Web Development Tutorials
Thanks for the tutorial. I m trying to delete a particular line of a file. Any help ?
Do the same steps and type a space in the replace tab.
Thank you so much. This is helping me quite a lot by taking the tediousness out of changing the navigation tree on my pages!
I'm glad that it helped you.


![[del.icio.us]](http://www.tutorialsroom.com/includes/bookmarkify/delicious.png)
![[Digg]](http://www.tutorialsroom.com/includes/bookmarkify/digg.png)
![[Facebook]](http://www.tutorialsroom.com/includes/bookmarkify/facebook.png)
![[Furl]](http://www.tutorialsroom.com/includes/bookmarkify/furl.png)
![[Google]](http://www.tutorialsroom.com/includes/bookmarkify/google.png)
![[MySpace]](http://www.tutorialsroom.com/includes/bookmarkify/myspace.png)
![[Spurl]](http://www.tutorialsroom.com/includes/bookmarkify/spurl.png)
![[Squidoo]](http://www.tutorialsroom.com/includes/bookmarkify/squidoo.png)
![[StumbleUpon]](http://www.tutorialsroom.com/includes/bookmarkify/stumbleupon.png)
![[Technorati]](http://www.tutorialsroom.com/includes/bookmarkify/technorati.png)
![[Yahoo!]](http://www.tutorialsroom.com/includes/bookmarkify/yahoo.png)
![[Email]](http://www.tutorialsroom.com/includes/bookmarkify/email.png)
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