next up previous contents
Next: sort Up: Unix Filters Previous: Unix Filters

grep

The name grep is short for ''generalized regular expression parser.'' The command grep is a UNIX filter that allows searches for regular expressions and fixed strings within ascii documents.

Regular expressions are patterns or templates that are defined by a combination of ascii strings and metacharacters. The metacharacters, characters that represent something other than their literal meaning, allow you to specify search tasks such as ''Find all strings that start at the beginning of a line that contain a character sequence with three g's in it''.

There is actually a family of grep commands, each command designed for a different task:

1.
grep Searches for limited regular expressions.
2.
egrep (Extended grep) Searches for full regular expressions.
3.
fgrep (Fixed string grep) Searches for fixed strings.
Which grep command you need to use depends on the complexity of the search task. fgrep is the command to use for strings that contain no wildcards, or other metacharacters, just a single text pattern that must be matched exactly. grep is the command to use for general purpose searching that requires the use of wildcards and other metacharacters, specifying string position, string size, character class, or closure. egrep is the extended version of grep. It can handle expressions just like the grep command, but it allows more variability in the search by allowing the search pattern to be ''string1 or string2, followed by string3 or string4''.

The general syntax for grep commands that search for regular expressions is:

grep <expression> <filename> [<filename>...]

The fgrep command is similar, except that it searches only for fixed strings:

fgrep <string(s)> <filename> [<filename>...]

Grep commands can be restricted to a single filename, or can be told to search a series of files, either by listing them in order, or by using wildcard characters.

For specific details on the specific metacharacters used and options available for the grep commands, see the online manual page for grep.


next up previous contents
Next: sort Up: Unix Filters Previous: Unix Filters
World Wide Web Administrator
1/5/1999