grep

GNU grep
Developer(s) GNU Project
Stable release 2.10 / November 16, 2011; 2 months ago (2011-11-16)
Development status Active
Operating system Linux, Mac OS, Solaris and Windows
Platform IA-32, x86-64, PowerPC and others
Available in English and others

grep is a command-line text-search utility originally written for Unix. The name comes from the ed command g/re/p (global / regular expression / print).[1][2] The grep command searches files or standard input globally for lines matching a given regular expression, and prints the lines to the program's standard output.

Contents

History

Grep was created by Ken Thompson as a standalone application adapted from the regular expression parser he had written for ed (which he also created). Its official creation date is given as March 3, 1973, in the Manual for Unix Version 4.

Usage

This is an example of a common usage of grep:

grep apple fruitlist.txt

In this case, grep prints all lines containing the sequence of characters apple from the file fruitlist.txt; therefore lines containing pineapple or apples are also printed. The pattern specified as a grep argument 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.

More than one file name may be specified as arguments to grep. For, example all files having the extension '.txt' in a given directory may be searched if the shell supports globbing by using an asterisk in place of the file name:

grep apple *.txt

Regular expressions can be used to match more complicated text patterns. The following prints all lines in the file that begin with the letter a, followed by any one character, followed by the letter sequence ple.

grep ^a.ple fruitlist.txt

As noted above, the name of 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 in an editor:

ed fruitlist.txt
g/^a.ple/p
q

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, the option flag -i enables case-insensitive search (ignore case).

grep -i apple fruitlist.txt

This prints all lines containing apple regardless of capitalization.

Selecting all lines containing apple as a word, i.e. surrounded' by white space (pineapple and apples do not match) may be accomplished with the -w option flag:

grep -w apple fruitlist.txt

But if fruitlist.txt contains apple as a word followed by hyphen (-) character, it is also matched.

cat fruitlist.txt
apple
apples
pineapple
apple-
apple-fruit
fruit-apple
 
grep -w apple fruitlist.txt
apple
apple-
apple-fruit
fruit-apple

Exact line match is performed with the -x option. Lines only containing exactly and solely apple are selected with a line-regexp instead of word-regexp:

cat fruitlist.txt
apple
apples
pineapple
apple-
apple-fruit
fruit-apple
 
grep -x apple fruitlist.txt
apple

The -v (lower-case v) option reverses the sense of the match and prints all lines that do not contain apple, as in this example.

grep -v apple fruitlist.txt
banana
pear
peach
orange

Variations

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 -E and -F in POSIX[3]). 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.

Other commands contain the word "grep" to indicate that they search (usually for regular expression matches). The pgrep utility, for instance, displays the processes whose names match a given regular expression.

In Perl, grep is the name of the built-in function that finds elements in a list that satisfy a certain property. This higher-order function is typically named filter in functional programming languages.

pcregrep is an implementation of grep that uses Perl regular expression syntax.

Ports of grep (within Cygwin and GnuWin32, for example) also run under Microsoft Windows. Some versions of Windows feature the similar qgrep command.[4]

Usage as a verb

In December 2003, the Oxford English Dictionary Online added draft entries for "grep" as both a noun and a verb.

A common verb usage is the phrase "You can't grep dead trees"—meaning one can more easily search through digital media, using tools such as grep, than one could with a hard copy (i.e., one made from dead trees, paper).[5] Compare with google.

See also

Notes

  1. ^ Hauben et al. 1997, Ch. 9
  2. ^ Raymond, Eric. "grep". Jargon File. http://www.catb.org/~esr/jargon/html/G/grep.html. Retrieved 2006-06-29. 
  3. ^ grep – Commands & Utilities Reference, The Single UNIX Specification, Issue 7 from The Open Group
  4. ^ Spalding, George (2000). Windows 2000 administration. Network professional's library. Osborne/McGraw-Hill. pp. 634. ISBN 9780078825828. http://books.google.com/books?id=fMVQAAAAMAAJ. Retrieved 2010-12-10. "QGREP.EXE[:] A similar tool to grep in UNIX, this tool can be used to search for a text string" 
  5. ^ Jargon File, article "Documentation"

References

  • Alain Magloire (August 2000). Grep: Searching for a Pattern. Iuniverse Inc. ISBN 0-595-10039-2. 
  • Hume, Andrew A tale of two greps, Software—Practice and Experience 18, ( 11 ), 1063–1072 ( 1988).
  • Hume, Andrew Grep wars: The strategic search initiative. In Peter Collinson, editor, Proceedings of the EUUG Spring 88 Conference, pages 237–245, Buntingford, UK, 1988. European UNIX User Group.
  • Michael Hauben et al. (April 1997). Netizens: On the History and Impact of Usenet and the Internet (Perspectives). Wiley-IEEE Computer Society Press. ISBN 978-0818677069. 

External links