Overview | My Computer or Windows Explorer | Dreamweaver | FrontPage | Linux/Perl
Overview
When maintaining Web pages, you often find that you need to make the same changes over and over within the same file, or within multiple files. The Search and Replace features of most Web authoring tools, and tools built in to popular operating systems, can quickly make these changes, and save you a lot of repetitive work.
Be advised that search and replace is dangerous as well as powerful. Make sure you have a backup before you do any site-wide search and replace, since there is no undo.
My Computer or Windows Explorer
Windows is limited to searching files; there is no replace feature. Windows can search by filename and by contents.
- Click Start, All Programs, Accessories, Windows Explorer.
- Select the folder you want to search.
- Click the Search button in the toolbar, or click View, Explorer Bar, Search, or press Ctrl-E.
- Click All files and folders.
- Click in the box labeled "All or part of the file name:" and type a to limit the
type of the file, for example
*.*htm*,*.txt
for all HTML and text files. - Click in the box labeled "A word or phrase in the file:" and type the text you want to search for.
- You can narrow your search by modification date, file size, or advanced options by clicking one of the three links.
- Click Search. You will see the results in the window to the right.
You can also search the HTML of files from the DOS command prompt.
- Click Start, Run. Type
cmd
. Press Enter. - Change to the directory where your files are stored, e.g.
cd w:\html\myfolder\
. - Type
find "string" *.html
to search all HTML files in the folder for a string. - For more information, type
help find
.
Dreamweaver
- Choose Edit, Find and Replace, or press Ctrl-F.
- Click the Help button for information about each of the options.
- From the Find In: menu, select the text or file(s) where you want to search. You can choose Selected Text, Current Document, Open Documents, Folder, Selected Files in Site, or Entire Current Local Site.
- From the Search: menu, select the part of the text or file(s) where
you want to search.
- Choose Source Code to search text in the Code view (HTML). This is useful for changing the addresses of links.
- Choose Text to search text as you see it in the design view. This is useful for changing text within the document, e.g. the name of a person or department. It treats formatted and unformatted text the same. This is the easiest way to search.
- Choose Text (Advanced) to include or exclude certain tags from the search.
- Choose Specific Tag to search only a certain tag or attribute, e.g. image tags or alt attributes.
- In the Find: box, type the text you want to find.
- In the Replace: box, type the text you want to replace.
- Check any of the options at the bottom that are needed.
- Check Match case to assure that the search is case sensitive and only finds exact matches.
- Check Match whole word to assure that the search does not replace the string within a word.
- Check Ignore white space to find strings even if they contain multiple spaces or span more than one line of HTML code.
- Check Use regular expression to use wildcards and other pattern matching, e.g. strings beginning or ending with some text or containing a certain number of characters. For more information, search Dreamweaver help for regular expressions. Regular expressions are powerful and should be used carefully.
- Click Find Next to find the next occurrence of the search text.
- Click Find All to find all occurrences and display a line of text or documents containing the search text in a Results window.
- Click Replace to replace the next occurrence of the search text.
- Click Replace All to replace all occurrences of the search text. The Results window will display each occurrence or file where the text was replaced.
Other Dreamweaver search and replace tutorials: Adobe, About.com, Lehigh, UWEC
Dreamweaver Regular Expressions
FrontPage 2003
- To find text, choose Edit, Find or press Ctrl-F. To replace text, choose Edit, Replace or press Ctrl-H.
- In the Find what: box, type the text you want to find.
- In the Replace with: box, type the text you want to replace.
- In the Find where: section, you can choose to look in the current page (the default), open page(s), selected page(s), or all pages in the current site.
- In the Direction: section, you can search above or below the cursor, or the entire document.
- In the Advanced: section, you can choose to match case, find whole word only, ignore whitespace differences, use regular expressions, and/or find in source code. These are similar to Dreamweaver features (described below).
- You can switch to the Find tab or Replace tab to change your action at any time.
- The HTML Tags tab allows you to search for a tag and replace or remove the tag and/or its contents.
- You can save a commonly used search and open it later using the Query: icons in the lower right.
- Click Find Next to find the next occurrence of the search text.
- Click Replace to replace the next occurrence of the search text.
- Click Replace All to replace all occurrences of the search text.
- Click Close to return to your document(s) when you are finished finding and replacing.
Linux shell (command line)
Linux search and replace commands are harder to learn and use than Windows applications, but they are much faster, especially when searching all Web pages on the server.
Search for text
- Move to the directory where your files are stored, to simplify commands.
cd /var/www/html/lib2/myfolder
- Use the grep command to search for strings in one or more files.
Every matching line in the file will be printed to the screen.
grep string filename
- Use the -i option to ignore case (find both lowercase and uppercase
versions of the string).
grep -i string filename1 filename2
- Use wildcards to restrict files in your search. For example,
*
matches any number of characters.grep string *.html
- Use regular expressions to match patterns of strings. For example,
^
and$
match the start and end of a line. (For more information, typeman grep
.)grep ^string$ *.html
- Use fgrep or grep -F to quickly search for a string
of fixed characters instead of a regular expression.
fgrep string *.html
- Use the -l option to display only the filenames of files that contain
the string.
grep -l string *.html
- Use the -H option to display file names with the matching text.
grep -H string *.html
- Redirect the results of the grep command to a file to store the
information for later viewing or further processing.
grep string filename > resultfile
more resultfile
grep -l string *.html > filelist
more filelist
- Use the find command to search for files in all subdirectories.
Use -name to search strings in filenames.
find . -name "*string*" -print
find . -name "*.html" -print
- Use the -exec option of the find command together with the grep
or fgrep command to search all files in all subdirectories for a string.
find . -name "*.html" -exec fgrep -l string '{}' ';'
Search and replace text
- Use perl to search and replace strings in multiple files.
perl -pi -e 's/Clam/Snook/g' *.html
- Use perl and the results of a find to replace strings only in files that contain
them.
find . -name "*.html" -exec fgrep -l Clam '{}' >> filelist ';'
more filelist
perl -pi -e 's/Clam/Snook/g' `cat filelist`
rm filelist