fgrep
   HOME

TheInfoList



OR:

grep is a
command-line A command-line interpreter or command-line processor uses a command-line interface (CLI) to receive commands from a user in the form of lines of text. This provides a means of setting parameters for the environment, invoking executables and pro ...
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, an ...
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 OS-9 is a family of real-time, process-based, multitasking, multi-user operating systems, developed in the 1980s, originally by Microware Systems Corporation for the Motorola 6809 microprocessor. It was purchased by Radisys Corp in 2001, an ...
.


History

Before it was named, grep was a private utility written by
Ken Thompson Kenneth Lane Thompson (born February 4, 1943) is an American pioneer of computer science. Thompson worked at Bell Labs for most of his career where he designed and implemented the original Unix operating system. He also invented the B programmi ...
to search files for certain patterns.
Doug McIlroy Malcolm Douglas McIlroy (born 1932) is a mathematician, engineer, and programmer. As of 2019 he is an Adjunct Professor of Computer Science at Dartmouth College. McIlroy is best known for having originally proposed Unix pipelines and developed s ...
, 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 assembly language 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 The term "Research Unix" refers to early versions of the Unix operating system for DEC PDP-7, PDP-11, VAX and Interdata 7/32 and 8/32 computers, developed in the Bell Labs Computing Sciences Research Center (CSRC). History The term ''Resear ...
. Stating that it is "generally cited as ''the'' prototypical software tool", McIlroy credited grep with "irrevocably ingraining" Thompson's
tools philosophy The Unix philosophy, originated by Ken Thompson, is a set of cultural norms and philosophical approaches to minimalist, modular software development. It is based on the experience of leading developers of the Unix operating system. Early Unix de ...
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 Seventh Edition Unix, also called Version 7 Unix, Version 7 or just V7, was an important early release of the Unix operating system. V7, released in 1979, was the last Bell Laboratories release to see widespread distribution before the commercial ...
. The "egrep" variant supports an extended regular expression syntax added by
Alfred Aho Alfred Vaino Aho (born August 9, 1941) is a Canadian computer scientist best known for his work on programming languages, compilers, and related algorithms, and his textbooks on the art and science of computer programming. Aho was elected into ...
after
Ken Thompson Kenneth Lane Thompson (born February 4, 1943) is an American pioneer of computer science. Thompson worked at Bell Labs for most of his career where he designed and implemented the original Unix operating system. He also invented the B programmi ...
'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 is typically named
filter Filter, filtering or filters may refer to: Science and technology Computing * Filter (higher-order function), in functional programming * Filter (software), a computer program to process a data stream * Filter (video), a software component tha ...
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 A port is a maritime facility comprising one or more wharves or loading areas, where ships load and discharge cargo and passengers. Although usually situated on a sea coast or estuary, ports can also be found far inland, such as H ...
of grep (within Cygwin 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 in ...
, for example) also run under Microsoft Windows. 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 supp ...
version 2. The , , and commands have also been ported to the IBM i operating system. The software Adobe InDesign 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 co ...
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 for paper).'' Jargon File'', article "Documentation"


See also

* Boyer–Moore string search algorithm * agrep, 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) In Unix-like and some other operating systems, find is a command-line utility that locates files based on some user-specified criteria and either prints the pathname of each matched object or, if another action is requested, performs that acti ...
, a Unix command that finds files by attribute, very different from grep *
List of Unix commands This is a list of Unix commands as specified by IEEE Std 1003.1-2008, which is part of the Single UNIX Specification (SUS). These commands can be found on Unix operating systems and most Unix-like operating systems. List See also * List of G ...
* 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 sou ...
, 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