Share on Facebook Share on Twitter Email
Answers.com

Grep

Did you mean: Grep (technology), grep (abbreviation), GREP (abbreviation)

 

(Global Regular Expression and Print) A Unix pattern matching utility that searches files for a string of text and outputs the line that contains the pattern. Grep came from ed, a Unix text editor, in which the command g/re/p meant "display all text in the file that matches this." That single function became a utility program itself, available in Unix, Linux, Windows and Mac environments.

The following example uses grep to find occurrences of the text "xfiles.lst" within any .CMD files in the default folder. The result shows the file name and the line of text that contains "xfiles.lst." See ed.


  grep command:  grep xfiles.lst *.cmd

  grep result:   File PREPCDPX.CMD
                 copy \stage\code\xfiles.lst
                 File SETSTAGE.CMD
                 copy \stage\code\xfiles.lst

Download Computer Desktop Encyclopedia to your iPhone/iTouch

Search unanswered questions...
Enter a question here...
Search: All sources Community Q&A Reference topics
Hacker Slang: grep
Top

[from the qed/ed editor idiom g/re/p, where re stands for a regular expression, to Globally search for the Regular Expression and Print the lines containing matches to it, via Unix grep(1)] To rapidly scan a file or set of files looking for a particular string or pattern (when browsing through a large set of files, one may speak of grepping around). By extension, to look for something by pattern. “Grep the bulletin board for the system backup schedule, would you?” See also vgrep.

[It has been alleged that the source is from the title of a paper “A General Regular Expression Parser”, but dmr confirms the g/re/p etymology --ESR]


Wikipedia: Grep
Top

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. [1] 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.

Contents

Usage

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.

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:

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:

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.

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

To solve this problem, a simple workaround is use grep again with ^ and $:

grep -w apple fruitlist.txt | grep -w ^apple$

Variations

There are countless implementations and derivatives of grep available for many operating systems. Early variants of grep included egrep and fgrep. The former applies an extended regular expression syntax that was added to Unix after Ken Thompson's original regular expression implementation. The latter searches for any of a list of fixed strings using the Aho-Corasick algorithm. These variants are embodied in most modern grep implementations as command-line switches (and standardized as -E and -F in POSIX[2]). 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.

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

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 a built-in function that finds elements in a list. In functional programming languages, this higher-order function is typically named "filter" instead.

The DOS, OS/2 and Microsoft Windows platforms provide the find command for simple string searches. Windows includes the "findstr" command which approximates much of the functionality of “grep”. Ports of grep (Cygwin and GnuWin32, for example) are also available for Windows.

Adobe added support for grep in the CS3 version of InDesign. Their support allow for finding and changing the formatting of text or the text itself in a document using grep[3].

Usage as a conversational 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)[4]. Compare with google. Visual grep is used as a term meaning to look through text searching for something, in the manner of the grep program.

Applications such as integrated development environments, text editors, and word processors often feature regular expression search, and sometimes refer to it as “grep”.

See also

References

  1. ^ Raymond, Eric. "grep". Jargon File. http://www.catb.org/~esr/jargon/html/G/grep.html. Retrieved 2006-06-29. 
  2. ^ grep – Commands & Utilities Reference, The Single UNIX® Specification, Issue 7 from The Open Group
  3. ^ See Adobe's site for usage [1]
  4. ^ Zaid Alawi. ""You Can't Grep Dead Trees"". http://frakkingoff.blogspot.com/2008/04/you-cant-grep-dead-trees.html. Retrieved 2009-06-09. 
  • 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.

External links


 
 

Did you mean: Grep (technology), grep (abbreviation), GREP (abbreviation)


 

Copyrights:

Computer Desktop Encyclopedia. THIS COPYRIGHTED DEFINITION IS FOR PERSONAL USE ONLY.
All other reproduction is strictly prohibited without permission from the publisher.
© 1981-2009 Computer Language Company Inc.  All rights reserved.  Read more
Hacker Slang. The Jargon File. Copyright © 2007.  Read more
Wikipedia. This article is licensed under the Creative Commons Attribution/Share-Alike License. It uses material from the Wikipedia article "Grep" Read more