Home > Resources > Web Development > Web Tips > Search and replace

Web Authoring: Search and Replace

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.

  1. Click Start, All Programs, Accessories, Windows Explorer.
  2. Select the folder you want to search.
  3. Click the Search button in the toolbar, or click View, Explorer Bar, Search, or press Ctrl-E.
  4. Click All files and folders.
  5. 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.
  6. Click in the box labeled "A word or phrase in the file:" and type the text you want to search for.
  7. You can narrow your search by modification date, file size, or advanced options by clicking one of the three links.
  8. 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.

  1. Click Start, Run. Type cmd. Press Enter.
  2. Change to the directory where your files are stored, e.g. cd w:\html\myfolder\.
  3. Type find "string" *.html to search all HTML files in the folder for a string.
  4. For more information, type help find.

Dreamweaver

  1. Choose Edit, Find and Replace, or press Ctrl-F.
  2. Click the Help button for information about each of the options.
  3. 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.
  4. From the Search: menu, select the part of the text or file(s) where you want to search.
    1. Choose Source Code to search text in the Code view (HTML). This is useful for changing the addresses of links.
    2. 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.
    3. Choose Text (Advanced) to include or exclude certain tags from the search.
    4. Choose Specific Tag to search only a certain tag or attribute, e.g. image tags or alt attributes.
  5. In the Find: box, type the text you want to find.
  6. In the Replace: box, type the text you want to replace.
  7. Check any of the options at the bottom that are needed.
    1. Check Match case to assure that the search is case sensitive and only finds exact matches.
    2. Check Match whole word to assure that the search does not replace the string within a word.
    3. Check Ignore white space to find strings even if they contain multiple spaces or span more than one line of HTML code.
    4. 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.
  8. Click Find Next to find the next occurrence of the search text.
  9. Click Find All to find all occurrences and display a line of text or documents containing the search text in a Results window.
  10. Click Replace to replace the next occurrence of the search text.
  11. 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

  1. To find text, choose Edit, Find or press Ctrl-F. To replace text, choose Edit, Replace or press Ctrl-H.
  2. In the Find what: box, type the text you want to find.
  3. In the Replace with: box, type the text you want to replace.
  4. 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.
  5. In the Direction: section, you can search above or below the cursor, or the entire document.
  6. 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).
  7. You can switch to the Find tab or Replace tab to change your action at any time.
  8. The HTML Tags tab allows you to search for a tag and replace or remove the tag and/or its contents.
  9. You can save a commonly used search and open it later using the Query: icons in the lower right.
  10. Click Find Next to find the next occurrence of the search text.
  11. Click Replace to replace the next occurrence of the search text.
  12. Click Replace All to replace all occurrences of the search text.
  13. 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

  1. Move to the directory where your files are stored, to simplify commands.
    • cd /var/www/html/lib2/myfolder
  2. 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
  3. Use the -i option to ignore case (find both lowercase and uppercase versions of the string).
    • grep -i string filename1 filename2
  4. Use wildcards to restrict files in your search. For example, * matches any number of characters.
    • grep string *.html
  5. Use regular expressions to match patterns of strings. For example, ^ and $ match the start and end of a line. (For more information, type man grep.)
    • grep ^string$ *.html
  6. Use fgrep or grep -F to quickly search for a string of fixed characters instead of a regular expression.
    • fgrep string *.html
  7. Use the -l option to display only the filenames of files that contain the string.
    • grep -l string *.html
  8. Use the -H option to display file names with the matching text.
    • grep -H string *.html
  9. 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
  10. 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
  11. 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

  1. Use perl to search and replace strings in multiple files.
    • perl -pi -e 's/Clam/Snook/g' *.html
  2. 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