HOME

TheInfoList



OR:

grep is a command-line utility for searching plain-text data sets for lines that match a
regular expression A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" ...
. Its name comes from the ed command ''g/re/p'' (''globally search for a regular expression and print matching lines''), which has the same effect. grep was originally developed for the
Unix Unix (; trademarked as UNIX) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, ...
operating system, but later available for all
Unix-like A Unix-like (sometimes referred to as UN*X or *nix) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Unix-li ...
systems and some others such as OS-9.


History

Before it was named, grep was a private utility written by Ken Thompson to search files for certain patterns. Doug McIlroy, unaware of its existence, asked Thompson to write such a program. Responding that he would think about such a utility overnight, Thompson actually corrected bugs and made improvements for about an hour on his own program called s (short for "search"). The next day he presented the program to McIlroy, who said it was exactly what he wanted. Thompson's account may explain the belief that grep was written overnight. Thompson wrote the first version in
PDP-11 The PDP-11 is a series of 16-bit minicomputers sold by Digital Equipment Corporation (DEC) from 1970 into the 1990s, one of a set of products in the Programmed Data Processor (PDP) series. In total, around 600,000 PDP-11s of all models were sol ...
assembly language In computer programming, assembly language (or assembler language, or symbolic machine code), often referred to simply as Assembly and commonly abbreviated as ASM or asm, is any low-level programming language with a very strong correspondence b ...
to help Lee E. McMahon analyze the text of ''
The Federalist Papers ''The Federalist Papers'' is a collection of 85 articles and essays written by Alexander Hamilton, James Madison, and John Jay under the collective pseudonym "Publius" to promote the ratification of the Constitution of the United States. The c ...
'' to determine authorship of the individual papers. The ed text editor (also authored by Thompson) had
regular expression A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" ...
support but could not be used on such a large amount of text, so Thompson excerpted that code into a standalone tool. He chose the name because in ed, the command ''g/re/p'' would print all lines matching a specified pattern. grep was first included in Version 4 Unix. Stating that it is "generally cited as ''the'' prototypical software tool", McIlroy credited grep with "irrevocably ingraining" Thompson's tools philosophy in Unix.


Implementations

A variety of grep implementations are available in many operating systems and software development environments. Early variants included egrep and fgrep, introduced in Version 7 Unix. The "egrep" variant supports an extended regular expression syntax added by Alfred Aho after Ken Thompson's original regular expression implementation. The "fgrep" variant searches for any of a list of ''fixed'' strings using the Aho–Corasick string matching algorithm. Binaries of these variants exist in modern systems, usually linking to grep or calling grep as a shell script with the appropriate flag added, e.g. exec grep -E "$@". egrep and fgrep, while commonly deployed on POSIX systems, to the point the POSIX specification mentions their widespread existence, are actually not part of POSIX. Other commands contain the word "grep" to indicate they are search tools, typically ones that rely on regular expression matches. The pgrep utility, for instance, displays the processes whose names match a given regular expression. In the
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
programming language, ''grep'' is the name of the built-in function that finds elements in a list that satisfy a certain property. This
higher-order function In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following: * takes one or more functions as arguments (i.e. a procedural parameter, which is a parameter of a procedure that is itse ...
is typically named filter or where in other languages. The pcregrep command is an implementation of grep that uses Perl regular expression syntax. Similar functionality can be invoked in the GNU version of grep with the -P flag. Ports of grep (within
Cygwin Cygwin ( ) is a POSIX-compatible programming and runtime environment that runs natively on Microsoft Windows. Under Cygwin, source code designed for Unix-like operating systems may be compiled with minimal modification and executed. The Cygwin in ...
and
GnuWin32 The G''nu''W''in''32 project provides native ports in the form of executable computer programs, patches, and source code for various GNU and open source tools and software, much of it modified to run on the 32-bit Windows platform. The ports inc ...
, for example) also run under
Microsoft Windows Windows is a group of several proprietary graphical operating system families developed and marketed by Microsoft. Each family caters to a certain sector of the computing industry. For example, Windows NT for consumers, Windows Server for ...
. Some versions of Windows feature the similar qgrep or findstr command. A grep command is also part of
ASCII ASCII ( ), abbreviated from American Standard Code for Information Interchange, is a character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices. Because ...
's ''MSX-DOS2 Tools'' for
MSX-DOS MSX-DOS is a discontinued disk operating system developed by Microsoft for the 8-bit home computer standard MSX, and is a cross between MS-DOS 1.25 and CP/M-80 2. MSX-DOS MSX-DOS and the extended BASIC with 3½-inch floppy disk suppo ...
version 2. The , , and commands have also been ported to the
IBM i IBM i (the ''i'' standing for ''integrated'') is an operating system developed by IBM for IBM Power Systems. It was originally released in 1988 as OS/400, as the sole operating system of the IBM AS/400 line of systems. It was renamed to i5/OS i ...
operating system. The software
Adobe InDesign Adobe InDesign is a desktop publishing and page layout designing software application produced by Adobe Inc. and first released in 1999. It can be used to create works such as posters, flyers, brochures, magazines, newspapers, presentations, b ...
has functions GREP (since CS3 version (2007)), in the ''find/change'' dialog box "GREP" tab, and introduced with InDesign CS4 in ''paragraph styles'' "GREP styles".


agrep

''a''grep (approximate grep) matches even when the text only ''approximately'' fits the search pattern. This following invocation finds ''netmasks'' in file ''myfile'', but also any other word that can be derived from it, given no more than two substitutions. agrep -2 netmasks myfile This example generates a list of matches with the closest, that is those with the fewest, substitutions listed first. The command flag ''B'' means ''best'': agrep -B netmasks myfile


Usage as a verb

In December 2003, the ''
Oxford English Dictionary The ''Oxford English Dictionary'' (''OED'') is the first and foundational historical dictionary of the English language, published by Oxford University Press (OUP). It traces the historical development of the English language, providing a c ...
Online'' added "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", which in this context is a
dysphemism A dysphemism is an expression with connotations that are derogatory either about the subject matter or to the audience. Dysphemisms contrast with neutral or euphemistic expressions. Dysphemism may be motivated by fear, distaste, hatred, contempt, ...
for paper).''
Jargon File The Jargon File is a glossary and usage dictionary of slang used by computer programmers. The original Jargon File was a collection of terms from technical cultures such as the MIT AI Lab, the Stanford AI Lab (SAIL) and others of the old ARPANET ...
'', article "Documentation"


See also

*
Boyer–Moore string search algorithm Boyer–Moore may refer to: * Boyer–Moore majority vote algorithm * Boyer–Moore string-search algorithm In computer science, the Boyer–Moore string-search algorithm is an efficient string-searching algorithm that is the standard benchmark ...
*
agrep agrep (approximate grep) is an open-source approximate string matching program, developed by Udi Manber and Sun Wu between 1988 and 1991, for use with the Unix operating system. It was later ported to OS/2, DOS, and Windows. It selects the best-s ...
, an approximate string-matching command *
find (Windows) In computing, find is a command in the command-line interpreters ( shells) of a number of operating systems. It is used to search for a specific text string in a file or files. The command sends the specified lines to the standard output device. ...
or Findstr, a DOS and Windows command that performs text searches, similar to a simple grep * find (Unix), a Unix command that finds files by attribute, very different from grep * List of Unix commands * vgrep, or "visual grep" *
ngrep ngrep (network grep) is a network packet analyzer written by Jordan Ritter. It has a command-line interface, and relies upon the pcap library and the GNU regex library. ngrep supports Berkeley Packet Filter ( BPF) logic to select network source ...
, the network grep


References

;Notes * * 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


GNU Grep official website


* *

- implementation details from GNU grep's author.
Command Grep – 25 practical examples
{{Authority control Unix text processing utilities Unix SUS2008 utilities Standard Unix programs Plan 9 commands Inferno (operating system) commands IBM i Qshell commands