Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Grep
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Unix command line utility for text search}} {{lowercase title}} {{Infobox software | name = grep | title = grep | screenshot = Grep example.png<!-- Image name is enough --> | caption = Example of grep command | screenshot size = 300px | screenshot alt = | collapsible = | author = [[Ken Thompson]]<ref name=history102>{{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}}</ref><ref>[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</ref> | developer = [[AT&T Bell Laboratories]] | released = {{Start date and age|1973|11}}<ref name=history102 /> | programming language = [[C (programming language)|C]] | operating system = [[Unix]], [[Unix-like]], [[Plan 9 from Bell Labs|Plan 9]], [[Inferno (operating system)|Inferno]], [[OS-9]], [[MSX-DOS]], [[IBM i]] | platform = | genre = [[Command (computing)|Command]] | license = | standard = | 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> ==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> 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> ==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> 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> 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> [[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 | 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 }}</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 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". ===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]]. ''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> This following invocation finds <code>netmasks</code> in file <code>myfile</code>, but also any other word that can be derived from it, given no more than two substitutions. <pre>agrep -2 netmasks myfile</pre> This example generates a list of matches with the closest, that is those with the fewest, substitutions listed first. The command flag <code>-B</code> means "best": <pre>agrep -B netmasks myfile</pre> ==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.<ref>{{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}}</ref> 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).<ref>''[[Jargon File]]'', article "Documentation"</ref> ==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]] * [[Visual inspection#Humorous terminology|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]] [[Category:Plan 9 commands]] [[Category:Inferno (operating system) commands]] [[Category:IBM i Qshell commands]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Anchor
(
edit
)
Template:Authority control
(
edit
)
Template:Cbignore
(
edit
)
Template:Cite AV media
(
edit
)
Template:Cite book
(
edit
)
Template:Cite conference
(
edit
)
Template:Cite journal
(
edit
)
Template:Cite magazine
(
edit
)
Template:Cite tech report
(
edit
)
Template:Cite web
(
edit
)
Template:Infobox software
(
edit
)
Template:Lowercase title
(
edit
)
Template:Main
(
edit
)
Template:Man
(
edit
)
Template:Plan 9 commands
(
edit
)
Template:Portal
(
edit
)
Template:R
(
edit
)
Template:Refbegin
(
edit
)
Template:Refend
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Unix commands
(
edit
)
Template:Wikibooks
(
edit
)