graphics\overbutt.gifSearching a file using grep

You can search a file for a specific text pattern using the Global Regular Expression Parser command (grep). You can use the grep command to search multiple files simultaneously. You must specify the pattern you want the grep command to search for, as well as one or more filenames on which the grep command can perform the search. The following example shows the grep command searching for the word "text" in the files file1 and file2. The search results display on screen.

Mabel~:$ grep text file1 file2

file1:This file contains text.

file1:You can highlight text using bold or italics.

file2:This file also contains text.

file2:Search for text and you can find it.

You can combine the grep command with other commands using pipes. For example, you can list files in your working directory and then use grep to search the list and return only filenames which contain a one. For information about combining commands, see "Combining commands with pipes."

Mabel~:$ ls | grep 1

file1

file12

file14

You can include wildcards in a search. For information about wildcards, see "Understanding wildcards." You can also specify command options with grep. For a list of available command options and other ways to use the grep command, see the Manual page for the grep command.

To search a file

1. At the command line, type,

grep [options] <pattern> [filenames...]

2. Press ENTER.

graphics\nicon.gif Notes