Grep
{{Short description|Unix command line utility for text search}}
{{lowercase title}}
{{Infobox software
| name = grep
| title = grep
| screenshot = Grep example.png
| caption = Example of grep command
| screenshot size = 300px
| screenshot alt =
| collapsible =
| author = Ken Thompson{{cite book|last=Kernighan|first=Brian|title=The Unix Programming Environment|year=1984|publisher=Prentice Hall|isbn=0-13-937681-X|pages=[https://archive.org/details/unixprogramminge0000kern/page/102 102]|url-access=registration|url=https://archive.org/details/unixprogramminge0000kern/page/102}}[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|url=https://web.archive.org/web/20150526111054/https://medium.com/@rualthanzauva/grep-was-a-private-command-of-mine-for-quite-a-while-before-i-made-it-public-ken-thompson-a40e24a5ef48 |date=2015-05-26 }}, By Benjamin Rualthanzauva, Published on Feb 5, 2014, Medium
| developer = AT&T Bell Laboratories
| released = {{Start date and age|1973|11}}
| 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 plaintext datasets for lines that match a regular expression. Its name comes from the ed command g/re/p
(global regular expression search and print), which has the same effect.Hauben et al. 1997, Ch. 9{{Cite web |url=http://www.catb.org/~esr/jargon/html/G/grep.html |title=grep |access-date=2006-06-29 |last=Raymond |first=Eric |author-link=Eric S. Raymond |work=Jargon File |url-status=dead |archive-url=https://web.archive.org/web/20060617052845/http://www.catb.org/~esr/jargon/html/G/grep.html |archive-date=2006-06-17 }} grep was originally developed for the Unix operating system, but later became available for all Unix-like systems and some others such as OS-9.{{cite book|author=Paul S. Dayan|year=1992|title=The OS-9 Guru - 1 : The Facts|publisher=Galactic Industrial Limited|isbn=0-9519228-0-7}}
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.{{cite AV media|url=https://www.youtube.com/watch?v=EY6q5dv_B-o |archive-url=https://ghostarchive.org/varchive/youtube/20211211/EY6q5dv_B-o| archive-date=2021-12-11 |url-status=live|title=VCF East 2019 -- Brian Kernighan interviews Ken Thompson|publication-date=6 May 2019|publisher=YouTube|type=video}}{{cbignore}} (35 mins)
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.Computerphile, [https://www.youtube.com/watch?v=NTfOnGZUZDk Where GREP Came From], interview with Brian Kernighan 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. He chose the name because in ed, the command g/re/p
would print all lines featuring a specified pattern match.{{cite web|url=http://perl.plover.com/classes/HoldSpace/samples/slide012.html|title=ed regexes|website=perl.plover.com|access-date=24 April 2018|url-status=dead|archive-url=https://web.archive.org/web/20171020031534/https://perl.plover.com/classes/HoldSpace/samples/slide012.html|archive-date=20 October 2017}}{{cite web|url=http://robots.thoughtbot.com/how-grep-got-its-name|title=How Grep Got its Name|website=robots.thoughtbot.com|access-date=24 April 2018|url-status=dead|archive-url=https://web.archive.org/web/20170809155158/https://robots.thoughtbot.com/how-grep-got-its-name|archive-date=9 August 2017}} 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.{{cite tech report |first1=M. D. |last1=McIlroy |author-link1=Doug McIlroy |year=1987 |url=http://www.cs.dartmouth.edu/~doug/reader.pdf |title=A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 |series=CSTR |number=139 |institution=Bell Labs |url-status=live |archive-url=https://web.archive.org/web/20171111151817/http://www.cs.dartmouth.edu/~doug/reader.pdf |archive-date=2017-11-11 }}
Implementations
A variety of grep implementations are available in many operating systems and software development environments.{{cite tech report |first=Tony |last=Abou-Assaleh |author2=Wei Ai|title=Survey of Global Regular Expression Print (GREP) Tools |institution=Dalhousie University|date=March 2004}} Early variants included egrep and fgrep, introduced in Version 7 Unix.{{r|reader}} The egrep variant supports an extended regular expression syntax added by Alfred Aho after Ken Thompson's original regular expression implementation.{{cite journal|last1=Hume|first1=Andrew|title=A Tale of Two Greps|journal=Software: Practice and Experience|date=1988|volume=18|issue=11|page=1063|doi=10.1002/spe.4380181105|s2cid=6395770}} The "fgrep" variant searches for any of a list of fixed strings using the Aho–Corasick string matching algorithm.{{cite book|last1=Meurant|first1=Gerard|title=Algorithms and Complexity|date=12 Sep 1990|publisher=Elsevier Science|page=278|isbn=9780080933917|url=https://books.google.com/books?id=6WriBQAAQBAJ|access-date=12 December 2015|url-status=live|archive-url=https://web.archive.org/web/20160304084311/https://books.google.com/books?id=6WriBQAAQBAJ&printsec=frontcover&source=gbs_ge_summary_r&cad=0|archive-date=4 March 2016}} 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.{{cite web|title=grep|url=http://pubs.opengroup.org/onlinepubs/009695399/utilities/grep.html|website=www.pubs.opengroup.org|publisher=The Open Group|access-date=12 December 2015|url-status=live|archive-url=https://web.archive.org/web/20151128184349/http://pubs.opengroup.org/onlinepubs/009695399/utilities/grep.html|archive-date=28 November 2015}}
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.{{cite web|title=pgrep(1)|url=http://linux.die.net/man/1/pgrep|website=www.linux.die.net|access-date=12 December 2015|url-status=live|archive-url=https://web.archive.org/web/20151222084135/http://linux.die.net/man/1/pgrep|archive-date=22 December 2015}}
In the Perl programming language, grep
is a built-in function that finds elements in a list that satisfy a certain property.{{cite web|title=grep|url=http://perldoc.perl.org/functions/grep.html|website=www.perldoc.perl.org|access-date=12 December 2015|url-status=live|archive-url=https://web.archive.org/web/20151207062445/http://perldoc.perl.org/functions/grep.html|archive-date=7 December 2015}} This higher-order function is typically named filter
or where
in other languages.
{{anchor|pcregrep}}The pcregrep command is an implementation of grep that uses Perl regular expression syntax.{{cite web|title=pcregrep man page|url=http://www.pcre.org/original/doc/html/pcregrep.html|website=www.pcre.org|publisher=University of Cambridge|access-date=12 December 2015|url-status=live|archive-url=https://web.archive.org/web/20151223035259/http://www.pcre.org/original/doc/html/pcregrep.html|archive-date=23 December 2015}} Similar functionality can be invoked in the GNU version of grep with the -P
flag.{{cite web|title=grep(1)|url=http://linux.die.net/man/1/grep|website=www.linux.die.net|access-date=12 December 2015|url-status=live|archive-url=https://web.archive.org/web/20151210004321/http://linux.die.net/man/1/grep|archive-date=10 December 2015}}
Ports of grep (within Cygwin and GnuWin32, for example) also run under Microsoft Windows. Some versions of Windows feature the similar qgrep or 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 = [https://archive.org/details/windows2000admin0000spal/page/634 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.{{cite web| url = https://archive.org/details/MSXDOS2TOOLS| title = MSX-DOS2 Tools User's Manual by ASCII Corporation| date = April 1993}}
The grep, egrep, and fgrep commands have also been ported to the IBM i operating system.{{cite web |title=IBM System i Version 7.2 Programming Qshell |language=en |author=IBM |website=IBM |author-link=IBM |url=https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc |access-date=2020-09-05 }}
The software Adobe InDesign has functions GREP (since CS3 version (2007){{cite web|url=https://creativepro.com/review-adobe-indesign-cs3/|title=Review: Adobe InDesign CS3 - CreativePro.com|date=20 April 2007|website=creativepro.com|access-date=24 April 2018|url-status=live|archive-url=https://web.archive.org/web/20180105233709/https://creativepro.com/review-adobe-indesign-cs3/|archive-date=5 January 2018}}), in the find/change dialog box{{cite web|title=InDesign Help: find/change|url=https://helpx.adobe.com/indesign/using/find-change.html|access-date=2016-08-12|url-status=live|archive-url=https://web.archive.org/web/20160828124223/https://helpx.adobe.com/indesign/using/find-change.html|archive-date=2016-08-28}} "GREP" tab, and introduced with InDesign CS4{{cite web |url=http://carijansen.com/tip-088/ |title=InDesign: GREP Styles (1) Setting text between parentheses in Italic |access-date=2018-01-05 |url-status=live |archive-url=https://web.archive.org/web/20170924230421/http://carijansen.com/tip-088/ |archive-date=2017-09-24 }} in paragraph styles{{cite web|title=InDesign Help: GREP styles|url=https://helpx.adobe.com/indesign/using/drop-caps-nested-styles.html#create_grep_styles|access-date=2016-08-12|url-status=live|archive-url=https://web.archive.org/web/20160828114627/https://helpx.adobe.com/indesign/using/drop-caps-nested-styles.html#create_grep_styles|archive-date=2016-08-28}} "GREP styles".
=agrep=
{{main|agrep}}
agrep (approximate grep) is an open-source approximate string matching program, developed by Udi Manber and Sun Wu between 1988 and 1991,{{cite conference |title=Agrep -- a fast approximate pattern-matching tool |last1=Wu |first1=Sun |last2=Manber |first2=Udi |date=20–24 January 1992 |location=San Francisco, California |conference=1992 Winter USENIX Conference |citeseerx = 10.1.1.89.5424}} 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.{{cite magazine |magazine=Sun Expert|author=S. Lee Henry |date=June 1998 |pages=35–26|title=Proper Searching}}
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<!-- "Google_(verb)" links here -->
In December 2003, the Oxford English Dictionary Online added "grep" as both a noun and a verb.{{Cite web|url=https://www.oed.com/information/updates/previous-updates/2003-2/december-2003/|title=New words list December 2003|access-date=2021-12-06|website=Oxford English Dictionary}}
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
{{Portal|Free and open-source software}}
- Boyer–Moore string-search algorithm
- agrep, an approximate string-matching command
- find (Windows) 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, the network grep
References
{{Reflist|30em}}
;Notes
{{refbegin}}
- {{Cite book|title=Grep: Searching for a Pattern|author=Alain Magloire|publisher=Iuniverse Inc|date=August 2000|isbn=0-595-10039-2}}
- 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.
- {{Cite book|title=Netizens: On the History and Impact of Usenet and the Internet (Perspectives)|author=Michael Hauben|author-link=Michael Hauben|publisher=Wiley-IEEE Computer Society Press|date=April 1997|isbn=978-0-8186-7706-9|display-authors=etal}}
{{refend}}
External links
{{Wikibooks|How To Search|grep}}
- [https://www.gnu.org/software/grep/ GNU Grep official website]
- [https://www.gnu.org/software/grep/manual/grep.html GNU Grep manual]
- {{man|1|grep|Plan 9}}
- {{man|1|grep|Inferno}}
- [http://lists.freebsd.org/pipermail/freebsd-current/2010-August/019310.html "why GNU grep is fast"] - implementation details from GNU grep's author.
- [https://www.linuxteacher.com/grep-command-in-unix-linux-with-examples/ Command Grep – 25 practical examples]
{{Unix commands}}
{{Plan 9 commands}}
{{Authority control}}
Category:Unix text processing utilities
Category:Unix SUS2008 utilities
Category:Standard Unix programs