Grep

Command-line utility for text search


title: "Grep" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["unix-text-processing-utilities", "unix-sus2008-utilities", "standard-unix-programs", "plan-9-commands", "inferno-(operating-system)-commands", "ibm-i-qshell-commands"] description: "Command-line utility for text search" topic_path: "technology/operating-systems" source: "https://en.wikipedia.org/wiki/Grep" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0

::summary Command-line utility for text search ::

::data[format=table title="Infobox software"]

FieldValue
namegrep
titlegrep
screenshotGrep example.png
captionExample of grep command
screenshot size300px
authorKen Thompson
developerAT&T Bell Laboratories
released
programming languageC
operating systemUnix, Unix-like, Plan 9, Inferno, OS-9, MSX-DOS, IBM i
genreCommand
::

| name = grep | title = grep | screenshot = Grep example.png | caption = Example of grep command | screenshot size = 300px | screenshot alt = | collapsible = | author = Ken Thompson | developer = AT&T Bell Laboratories | released = | programming language = C | operating system = Unix, Unix-like, Plan 9, Inferno, OS-9, MSX-DOS, IBM i | platform = | genre = Command | license = | standard = | AsOf = grep is a command-line utility for searching text for lines that match a regular expression. Its name comes from the ed command g/*re*/p (global, regular expression, print), which has the same effect.{{cite book |last1=Hauben |first1=Michael |author-link1=Michael Hauben |last2=Hauben |first2=Ronda |date=1997 |title=Netizens: On the History and Impact of Usenet and the Internet |chapter=On the Early History and Impact of Unix Tools to Build the Tools for a New Millennium |isbn=978-0-8186-7706-9 |url=http://www.columbia.edu/~rh120 |location=Los Alamitos, California |publisher=IEEE Computer Society Press |quote=One afternoon I asked Ken Thompson if he could lift the regular expression recognizer out of the editor and make a one-pass program to do it. He said yes. The next morning I found a note in my mail announcing a program named grep. It worked like a charm. When asked what that funny name meant, Ken said it was obvious. It stood for the editor command that it simulated, g/re/p (global regular expression print). |quote-page=136

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 assembly language to help Lee E. McMahon analyze the text of The Federalist Papers to determine authorship of the individual papers. The ed text editor (also authored by Thompson) had regular expression support but could not be used to search through such a large amount of text, as it loaded the entire file into memory to enable random access editing, so Thompson excerpted that regexp code into a standalone tool which would instead process arbitrarily long files sequentially without buffering too much into memory. 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. . Commands 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](pgrep) utility, for instance, displays the processes whose names match a given regular expression.

In the Perl programming language, grep is a built-in function that finds elements in a list that satisfy a certain property. This higher-order function is typically named [filter](filter-higher-order-function) or where in other languages.

pcregrepThe 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 and GnuWin32, for example) also run under Microsoft Windows. Some versions of Windows feature the similar qgrep or [findstr](findstr) command.{{cite book | last = Spalding | first = George | title = Windows 2000 administration | url = https://archive.org/details/windows2000admin0000spal | url-access = registration | access-date = 2010-12-10 | series = Network professional's library | year = 2000 | publisher = Osborne/McGraw-Hill | isbn = 978-0-07-882582-8 | pages = 634 | quote = QGREP.EXE[:] A similar tool to grep in UNIX, this tool can be used to search for a text string

A grep command is also part of ASCII's MSX-DOS2 Tools for MSX-DOS version 2.

The grep, egrep, and fgrep 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

Main article: 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.

agrep 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 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).

References

;Notes

  • {{cite journal |last1=Hume |first1=Andrew |title=Grep wars: The strategic search initiative |editor-last1=Collinson |editor-first1=Peter |journal=Proceedings of the EUUG Spring 88 Conference |location=Buntingford, UK |date=1988 |publisher=European UNIX User Group |pages=237–245

References

  1. Kernighan, Brian. (1984). "The Unix Programming Environment". Prentice Hall.
  2. [https://medium.com/@rualthanzauva/grep-was-a-private-command-of-mine-for-quite-a-while-before-i-made-it-public-ken-thompson-a40e24a5ef48 “grep was a private command of mine for quite a while before i made it public.” -Ken Thompson] {{webarchive. link. (2015-05-26 , By Benjamin Rualthanzauva, Published on Feb 5, 2014, Medium)
  3. Raymond, Eric. "grep". Jargon File.
  4. Paul S. Dayan. (1992). "The OS-9 Guru - 1 : The Facts". Galactic Industrial Limited.
  5. "VCF East 2019 -- Brian Kernighan interviews Ken Thompson". YouTube.
  6. Computerphile, ''[https://www.youtube.com/watch?v=NTfOnGZUZDk Where GREP Came From]'', interview with [[Brian Kernighan]]
  7. "ed regexes".
  8. "How Grep Got its Name".
  9. (1987). "A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986".
  10. Abou-Assaleh, Tony. (March 2004). "Survey of Global Regular Expression Print (GREP) Tools".
  11. (1988). "A Tale of Two Greps". Software: Practice and Experience.
  12. (12 Sep 1990). "Algorithms and Complexity". Elsevier Science.
  13. "grep". The Open Group.
  14. "pgrep(1)".
  15. "grep".
  16. "pcregrep man page". University of Cambridge.
  17. "grep(1)".
  18. (April 1993). "MSX-DOS2 Tools User's Manual by ASCII Corporation".
  19. IBM. "IBM System i Version 7.2 Programming Qshell".
  20. (20 April 2007). "Review: Adobe InDesign CS3 - CreativePro.com".
  21. "InDesign Help: find/change".
  22. "InDesign: GREP Styles (1) Setting text between parentheses in Italic".
  23. "InDesign Help: GREP styles".
  24. (20–24 January 1992). "Agrep -- a fast approximate pattern-matching tool".
  25. S. Lee Henry. (June 1998). "Proper Searching".
  26. "New words list December 2003".
  27. ''[[Jargon File]]'', article "Documentation"

::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::

unix-text-processing-utilitiesunix-sus2008-utilitiesstandard-unix-programsplan-9-commandsinferno-(operating-system)-commandsibm-i-qshell-commands