Grep: Difference between revisions

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
Fixing the tiny screenshot. Remove the vague "Platform" parameter; the "OS" parameter already has a comprehensive answer.
 
imported>Minerva P. Malveen
Vandalism, Undid revision 1322830778 by ~2025-34094-14 (talk)
 
Line 1: Line 1:
{{Short description|Unix command line utility for text search}}
{{Short description |Command-line utility for text search}}
{{lowercase title}}
{{lowercase title}}
{{Infobox software
{{Infobox software
Line 20: Line 20:
| AsOf =  
| AsOf =  
}}
}}
'''grep''' is a [[command line interface|command-line]] utility for searching plaintext datasets for lines that match a [[regular expression]]. Its name comes from the [[ed (text editor)|ed]] command <code>g/re/p</code> (global regular expression search and print), which has the same effect.<ref>Hauben et al. 1997, Ch. 9</ref><ref name="etymology">{{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 }}</ref> 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]].<ref>{{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}}</ref>
'''grep''' is a [[command line interface|command-line]] utility for searching [[plaintext]] for lines that match a [[regular expression]]. Its name comes from the [[ed (text editor)|ed]] command <code>g/''re''/p</code> ('''g'''lobal, '''r'''egular '''e'''xpression, '''p'''rint), which has the same effect.<ref>{{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
}}</ref><ref name="etymology">{{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 }}</ref> grep was originally developed for the [[Unix]] operating system, and is commonly available on [[Unix-like]] and some other systems such as [[OS-9]].<ref>{{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}}</ref>


==History==
==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.<ref>{{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)</ref>
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 <code>s</code> (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.<ref>{{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)</ref>


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.<ref>Computerphile, ''[https://www.youtube.com/watch?v=NTfOnGZUZDk Where GREP Came From]'', interview with [[Brian Kernighan]]</ref> The [[ed (text editor)|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.<ref name=history102 /> He chose the name because in ed, the command <code>g/re/p</code> would print all lines featuring a specified pattern match.<ref>{{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}}</ref><ref>{{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}}</ref> 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.<ref name="reader">{{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 }}</ref>
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.<ref>Computerphile, ''[https://www.youtube.com/watch?v=NTfOnGZUZDk Where GREP Came From]'', interview with [[Brian Kernighan]]</ref> The [[ed (text editor)|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.<ref name=history102 /> He chose the name because in ed, the command <code>g/''re''/p</code>, where the ''<code>re</code>'' is the '''r'''egular '''e'''xpression to match, would print all lines featuring a specified pattern match.<ref>{{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}}</ref><ref>{{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}}</ref> 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.<ref name="reader">{{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 }}</ref>


==Implementations==
==Implementations==
A variety of grep implementations are available in many operating systems and software development environments.<ref>{{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}}</ref> Early variants included egrep and fgrep, introduced in [[Version 7 Unix]].{{r|reader}} The egrep variant supports an [[Regular expression#POSIX basic and extended|extended regular expression]] syntax added by [[Alfred Aho]] after [[Ken Thompson]]'s original regular expression implementation.<ref name=Huma1988>{{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}}</ref> The "fgrep" variant searches for any of a list of ''fixed'' strings using the [[Aho–Corasick string matching algorithm]].<ref name=Meurant1990>{{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}}</ref> 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. <code>exec grep -E "$@"</code>. 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.<ref>{{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}}</ref>
A variety of grep implementations are available in many operating systems and software development environments.<ref>{{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}}</ref> Early variants included <code>egrep</code> and <code>fgrep</code>, introduced in [[Version 7 Unix]].{{r|reader}} The <code>egrep</code> variant supports an [[Regular expression#POSIX basic and extended|extended regular expression]] syntax added by [[Alfred Aho]] after [[Ken Thompson]]'s original regular expression implementation.<ref name=Huma1988>{{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}}</ref> The <code>fgrep</code> variant searches for any of a list of ''fixed'' strings using the [[Aho–Corasick string matching algorithm]].<ref name=Meurant1990>{{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}}</ref> 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. {{code|exec grep -E "$@"|bash}}. Commands <code>egrep</code> and <code>fgrep</code>, while commonly deployed on POSIX systems, to the point the POSIX specification mentions their widespread existence, are actually not part of POSIX.<ref>{{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}}</ref>


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.<ref>{{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}}</ref>
Other commands contain the word "grep" to indicate they are search tools, typically ones that rely on regular expression matches. The <code>[[pgrep]]</code> utility, for instance, displays the processes whose names match a given regular expression.<ref>{{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}}</ref>


In the [[Perl]] programming language, <code>grep</code> is a built-in function that finds elements in a list that satisfy a certain property.<ref>{{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}}</ref> This [[higher-order function]] is typically named <code>[[filter (higher-order function)|filter]]</code> or <code>where</code> in other languages.
In the [[Perl]] programming language, <code>grep</code> is a built-in function that finds elements in a list that satisfy a certain property.<ref>{{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}}</ref> This [[higher-order function]] is typically named <code>[[filter (higher-order function)|filter]]</code> or <code>where</code> in other languages.


{{anchor|pcregrep}}The pcregrep command is an implementation of grep that uses [[Regular expression#Perl and PCRE|Perl regular expression]] syntax.<ref>{{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}}</ref> Similar functionality can be invoked in the GNU version of grep with the <code>-P</code> flag.<ref>{{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}}</ref>
{{anchor|pcregrep}}The <code>pcregrep</code> command is an implementation of grep that uses [[Regular expression#Perl and PCRE|Perl regular expression]] syntax.<ref>{{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}}</ref> Similar functionality can be invoked in the GNU version of grep with the <code>-P</code> flag.<ref>{{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}}</ref>


[[Porting|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.<ref>{{cite book
[[Porting|Ports]] of grep (within [[Cygwin]] and [[GnuWin32]], for example) also run under [[Microsoft Windows]]. Some versions of Windows feature the similar <code>qgrep</code> or <code>[[findstr]]</code> command.<ref>{{cite book
| last      = Spalding
| last      = Spalding
| first      = George
| first      = George
Line 53: Line 65:
A grep command is also part of [[ASCII Corporation|ASCII]]'s ''MSX-DOS2 Tools'' for [[MSX-DOS]] version 2.<ref>{{cite web| url = https://archive.org/details/MSXDOS2TOOLS| title = MSX-DOS2 Tools User's Manual by ASCII Corporation| date = April 1993}}</ref>
A grep command is also part of [[ASCII Corporation|ASCII]]'s ''MSX-DOS2 Tools'' for [[MSX-DOS]] version 2.<ref>{{cite web| url = https://archive.org/details/MSXDOS2TOOLS| title = MSX-DOS2 Tools User's Manual by ASCII Corporation| date = April 1993}}</ref>


The grep, egrep, and fgrep commands have also been ported to the [[IBM i]] operating system.<ref>{{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 }}</ref>
The <code>grep</code>, <code>egrep</code>, and <code>fgrep</code> commands have also been ported to the [[IBM i]] operating system.<ref>{{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 }}</ref>


The software [[Adobe InDesign]] has functions GREP (since CS3 version (2007)<ref>{{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}}</ref>), in the ''find/change'' dialog box<ref>{{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}}</ref> "GREP" tab, and introduced with InDesign CS4<ref>{{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 }}</ref> in ''paragraph styles''<ref>{{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}}</ref> "GREP styles".
The software [[Adobe InDesign]] has functions GREP (since CS3 version (2007)<ref>{{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}}</ref>), in the ''find/change'' dialog box<ref>{{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}}</ref> "GREP" tab, and introduced with InDesign CS4<ref>{{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 }}</ref> in ''paragraph styles''<ref>{{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}}</ref> "GREP styles".
Line 59: Line 71:
===agrep===
===agrep===
{{main|agrep}}
{{main|agrep}}
'''agrep''' (approximate grep) is an [[open-source software|open-source]] [[approximate string matching]] program, developed by [[Udi Manber]] and Sun Wu between 1988 and 1991,<ref>{{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}}</ref> for use with the [[Unix]] operating system. It was later ported to [[OS/2]], [[DOS]], and [[Microsoft Windows|Windows]].
'''<code>agrep</code>''' (approximate grep) is an [[open-source software|open-source]] [[approximate string matching]] program, developed by [[Udi Manber]] and Sun Wu between 1988 and 1991,<ref>{{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}}</ref> for use with the [[Unix]] operating system. It was later ported to [[OS/2]], [[DOS]], and [[Microsoft Windows|Windows]].


''a''grep matches even when the text only ''approximately'' fits the search pattern.<ref name=eGrep.SunX>{{cite magazine |magazine=Sun Expert|author=S. Lee Henry |date=June 1998 |pages=35–26|title=Proper Searching}}</ref>
''a''grep matches even when the text only ''approximately'' fits the search pattern.<ref name=eGrep.SunX>{{cite magazine |magazine=Sun Expert|author=S. Lee Henry |date=June 1998 |pages=35–26|title=Proper Searching}}</ref>
Line 75: Line 87:
==See also==
==See also==
{{Portal|Free and open-source software}}
{{Portal|Free and open-source software}}
* [[agrep]], an approximate string-matching command
* [[Boyer–Moore string-search algorithm]]
* [[Boyer–Moore string-search algorithm]]
* [[agrep]], an approximate string-matching command
* [[find (Unix)]], a Unix command that finds files; unlike grep
* [[find (Windows)]] or [[Findstr]], a DOS and Windows command that performs text searches, similar to a simple grep
* [[find (Windows)]] or [[findstr]], a DOS/Windows command that searches for text in files; similar to grep
* [[find (Unix)]], a Unix command that finds files by attribute, very different from grep
* [[List of POSIX commands]]
* [[List of Unix commands]]
* [[Visual inspection#Humorous terminology |vgrep]], or "visual grep"
* [[Visual inspection#Humorous terminology|vgrep]], or "visual grep"
* [[ngrep]], the network grep
* [[ngrep]], the network grep


Line 88: Line 100:
{{refbegin}}
{{refbegin}}
* {{Cite book|title=Grep: Searching for a Pattern|author=Alain Magloire|publisher=Iuniverse Inc|date=August 2000|isbn=0-595-10039-2}}
* {{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 journal
* {{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}}
|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
}}
{{refend}}
{{refend}}



Latest revision as of 18:42, 18 November 2025

Template:Short description Template:Lowercase title Script error: No such module "Infobox".Template:Template otherScript error: No such module "Check for unknown parameters".Template:Main other grep is a command-line utility for searching plaintext 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.[1][2] grep was originally developed for the Unix operating system, and is commonly available on Unix-like and some other systems such as OS-9.[3]

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.[4]

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.[5] 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.[6] He chose the name because in ed, the command g/re/p, where the re is the regular expression to match, would print all lines featuring a specified pattern match.[7][8] 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.[9]

Implementations

A variety of grep implementations are available in many operating systems and software development environments.[10] Early variants included egrep and fgrep, introduced in Version 7 Unix.Template:R The egrep variant supports an extended regular expression syntax added by Alfred Aho after Ken Thompson's original regular expression implementation.[11] The fgrep variant searches for any of a list of fixed strings using the Aho–Corasick string matching algorithm.[12] 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 "$@". 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.[13]

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.[14]

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

Script error: No such module "anchor".The pcregrep command is an implementation of grep that uses Perl regular expression syntax.[16] Similar functionality can be invoked in the GNU version of grep with the -P flag.[17]

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.[18]

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

The grep, egrep, and fgrep commands have also been ported to the IBM i operating system.[20]

The software Adobe InDesign has functions GREP (since CS3 version (2007)[21]), in the find/change dialog box[22] "GREP" tab, and introduced with InDesign CS4[23] in paragraph styles[24] "GREP styles".

agrep

Script error: No such module "Labelled list hatnote". agrep (approximate grep) is an open-source approximate string matching program, developed by Udi Manber and Sun Wu between 1988 and 1991,[25] 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.[26]

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.[27]

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).[28]

See also

Script error: No such module "Portal".

References

Template:Reflist

Notes

Template:Refbegin

  • Script error: No such module "citation/CS1".
  • Script error: No such module "Citation/CS1".

Template:Refend

External links

Template:Sister project

Script error: No such module "Navbox". Template:Plan 9 commands

Template:Authority control

  1. Script error: No such module "citation/CS1".
  2. Script error: No such module "citation/CS1".
  3. Script error: No such module "citation/CS1".
  4. Script error: No such module "Citation/CS1".Template:Cbignore (35 mins)
  5. Computerphile, Where GREP Came From, interview with Brian Kernighan
  6. Cite error: Invalid <ref> tag; no text was provided for refs named history102
  7. Script error: No such module "citation/CS1".
  8. Script error: No such module "citation/CS1".
  9. Script error: No such module "citation/CS1".
  10. Script error: No such module "citation/CS1".
  11. Script error: No such module "Citation/CS1".
  12. Script error: No such module "citation/CS1".
  13. Script error: No such module "citation/CS1".
  14. Script error: No such module "citation/CS1".
  15. Script error: No such module "citation/CS1".
  16. Script error: No such module "citation/CS1".
  17. Script error: No such module "citation/CS1".
  18. Script error: No such module "citation/CS1".
  19. Script error: No such module "citation/CS1".
  20. Script error: No such module "citation/CS1".
  21. Script error: No such module "citation/CS1".
  22. Script error: No such module "citation/CS1".
  23. Script error: No such module "citation/CS1".
  24. Script error: No such module "citation/CS1".
  25. Script error: No such module "citation/CS1".
  26. Template:Cite magazine
  27. Script error: No such module "citation/CS1".
  28. Jargon File, article "Documentation"