grep is a command line text search utility originally written for Unix. The name is taken from the first letters in global / regular expression / print, a series of instructions in text editors such as ed. A backronym of the unusual name also exists in the form of Generalized Regular Expression Parser. The
grep command searches files or standard input globally for lines matching a given regular expression, and prints them to the program's standard output.
Grep was created by Ken Thompson as a standalone application adapted from the regular expression parser he had written for the ed editor (which he also created). The name grep comes from the ed editor command it simulated, g/re/p (global regular expression print). Its official date of creation is given as March 3, 1973 in the Manual for Unix Version 4.
This is an example of a common grep usage:
grep apple fruitlist.txt
In this case, grep prints all lines containing apple from the file fruitlist.txt, regardless of word boundaries; therefore lines containing pineapple or apples are also printed. The grep command is case sensitive by default, so this example's output does not include lines containing Apple (with a capital A) unless they also contain apple.
To search all .txt files in a directory for apple in a shell that supports globbing, use an asterisk in place of the file name:
Regular expressions can be used to match more complicated queries. The following prints all lines in the file that begin with the letter a, followed by any one character, then the letters ple.
grep ^a.ple fruitlist.txt
As noted above, the term "grep" derives from a usage in ed and related text editors. Before grep existed as a separate command, the same effect might have been achieved by doing:
where the second line is the command given to ed to print the relevant lines, and the third line is the command to exit from ed.
Like most Unix commands, grep accepts options in the form of command-line arguments, to change many of its behaviors. For example:
grep -i apple fruitlist.txt
This prints all lines containing apple regardless of capitalization. The -i argument tells grep to be case insensitive, or to ignore case.
To print all lines containing apple as a word (pineapple and apples will not match):
grep -w apple fruitlist.txt
But if fruitlist.txt contains apple word followed by hyphen (-) character, it will also get matched.
grep -w apple fruitlist.txt
the -v (lower-case v) prints all lines that do NOT contain apple in this example.
grep -v apple fruitlist.txt
There are countless implementations and derivatives of grep available for many operating systems. Early variants of grep included egrep and fgrep.
egrep applies an extended regular expression syntax that was added to Unix after Ken Thompson's original regular expression implementation.
fgrep searches for any of a list of fixed strings using the Aho–Corasick string matching algorithm. These variants of
grep persist in most modern grep implementations as command-line switches (and standardized as
-F in POSIX). In such combined implementations, grep may also behave differently depending on the name by which it is invoked, allowing fgrep, egrep, and grep to be links to the same program.
Full article ▸