Grep

From Wikipedia, the free encyclopedia

The correct title of this article is grep. The initial letter is shown capitalized due to technical restrictions.

grep is a command line utility that was originally written for use with the Unix operating system. Given a list of files or standard input to read, grep searches for lines of text that match one or many regular expressions, and outputs only the matching lines.

Contents

The name, "grep", derives from the command used to perform a similar operation, using the Unix text editor ed:

g/re/p

which means "search globally for lines matching the regular expression, and print them". There are various command line switches available when using grep that modify this default behavior including printing lines which do not match, finding or excluding files to search, and annotating the output in various ways. There are also multiple, modern implementations of the classic Unix grep, each with a unique feature set.

This is an example of a common grep usage:

grep apple fruitlist.txt

Grep would return, in this case, all of the lines in file fruitlist.txt with an instance of apple in them. Keep in mind that grep would not return lines with Apple (capital A) because by default grep is case sensitive. Like most Unix commands grep accepts flags which can be used to change this and many other behaviors. For example:

grep -i apple fruitlist.txt

This would return all lines with the words 'apple', 'Apple', 'apPLE', or any other mixing of captial and lower case.

The option -e can also be used to declare multiple patterns for searching on the command line with some versions of grep.

Also known as (AKA) General Regular Expression Parser (GREP).

There are many derivatives of grep, for example agrep which stands for approximate grep to facilitate fuzzy string searching, fgrep for fixed pattern searches, and egrep for searches involving more sophisticated regular expression syntax. fgrep and egrep are typically the same program as grep, which behaves differently depending on the name by which it is invoked. Tcgrep is a rewrite of grep which uses Perl regular expression syntax. All these variations of grep have been ported to many computer operating systems.

Many other commands contain the word "grep." pgrep, for instance, displays the processes whose names match a regular expression.

In Perl, grep is a built-in function, which when provided both a regular expression (or a general code block) and a list, returns the elements of that list matched by the expression. In functional programming languages, this higher-order function is typically named "filter" instead.

The Microsoft Windows platform provides a utility called "findstr" which approximates much of the functionality of "grep."

As the name "grep" neatly fits the phonology of English, it is often used as a verb, meaning to search – usually, to search a known set of files, as one would with the grep utility. The direct object is the set of files searched: "Kibo grepped his Usenet spool for his name." Compare with google. Sometimes visual grep is used as a term meaning to look through text searching for something, in the manner of the grep program.

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

A common usage is the phrase "You can't grep dead trees" - meaning computerized documentation is better than printed documentation because computers can search documents by using tools such as grep.

The word "grep" has also become a synonym for regular expressions themselves. Many text and word processors now employ regular expression search features, which those applications will often refer to as a "grep tool" or "grep mode" in which one creates "grep patterns", causing confusion, especially in non-Unix environments.

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 simply reads a set of fixed strings to search for from a file. These early variants are embodied in most modern grep implementations as simple command-line switches (e.g. -E and -F respectively in GNU grep).

  • Alain Magloire (August, 2000). Grep: Searching for a Pattern. Iuniverse Inc. ISBN 0-595-10039-2. 
  • Andrew Hume. 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.
  • Raymond, Eric. grep. Jargon File. Retrieved on 2006-06-29.

Wikibooks
Wikibooks How to search has more about this subject:


Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.