Grep Explained

grep
grep
Author:Ken Thompson[1] [2]
Developer:AT&T Bell Laboratories
Programming Language:C
Operating System:Unix, Unix-like, Plan 9, Inferno, OS-9, MSX-DOS, IBM i
Platform:Cross-platform
Genre:Command

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.[3] [4] grep was originally developed for the Unix operating system, but later available for all Unix-like systems and some others such as OS-9.[5]

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

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.[7] 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.[8] [9] 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.[10]

Implementations

A variety of grep implementations are available in many operating systems and software development environments.[11] 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.[12] The "fgrep" variant searches for any of a list of fixed strings using the Aho–Corasick string matching algorithm.[13] 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.[14]

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

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

The pcregrep command is an implementation of grep that uses Perl regular expression syntax.[17] Similar functionality can be invoked in the GNU version of grep with the -P flag.[18]

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

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

The,, and commands have also been ported to the IBM i operating system.[21]

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

agrep

See main article: agrep. agrep (approximate grep) 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 myfileThis 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

References

Notes

External links

Notes and References

  1. Book: Kernighan, Brian. The Unix Programming Environment. 1984. Prentice Hall. 0-13-937681-X. 102. registration.
  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
  3. Hauben et al. 1997, Ch. 9
  4. Web site: grep . 2006-06-29 . Raymond . Eric . Eric S. Raymond . Jargon File . dead . https://web.archive.org/web/20060617052845/http://www.catb.org/~esr/jargon/html/G/grep.html . 2006-06-17 .
  5. Book: Paul S. Dayan. 1992. The OS-9 Guru - 1 : The Facts. Galactic Industrial Limited. 0-9519228-0-7.
  6. VCF East 2019 -- Brian Kernighan interviews Ken Thompson. https://ghostarchive.org/varchive/youtube/20211211/EY6q5dv_B-o. 2021-12-11 . live. 6 May 2019. YouTube. video. (35 mins)
  7. Computerphile, Where GREP Came From, interview with Brian Kernighan
  8. Web site: ed regexes. perl.plover.com. 24 April 2018. dead. https://web.archive.org/web/20171020031534/https://perl.plover.com/classes/HoldSpace/samples/slide012.html. 20 October 2017.
  9. Web site: How Grep Got its Name. robots.thoughtbot.com. 24 April 2018. dead. https://web.archive.org/web/20170809155158/https://robots.thoughtbot.com/how-grep-got-its-name. 9 August 2017.
  10. M. D. . McIlroy . Doug McIlroy . 1987 . A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 . CSTR . 139 . Bell Labs . live . https://web.archive.org/web/20171111151817/http://www.cs.dartmouth.edu/~doug/reader.pdf . 2017-11-11 .
  11. Tony . Abou-Assaleh . Wei Ai. Survey of Global Regular Expression Print (GREP) Tools . Dalhousie University. March 2004.
  12. Hume. Andrew. A Tale of Two Greps. Software: Practice and Experience. 1988. 18. 11. 1063. 10.1002/spe.4380181105. 6395770.
  13. Book: Meurant. Gerard. Algorithms and Complexity. 12 Sep 1990. Elsevier Science. 278. 9780080933917. 12 December 2015. live. https://web.archive.org/web/20160304084311/https://books.google.com/books?id=6WriBQAAQBAJ&printsec=frontcover&source=gbs_ge_summary_r&cad=0. 4 March 2016.
  14. Web site: grep. www.pubs.opengroup.org. The Open Group. 12 December 2015. live. https://web.archive.org/web/20151128184349/http://pubs.opengroup.org/onlinepubs/009695399/utilities/grep.html. 28 November 2015.
  15. Web site: pgrep(1). www.linux.die.net. 12 December 2015. live. https://web.archive.org/web/20151222084135/http://linux.die.net/man/1/pgrep. 22 December 2015.
  16. Web site: grep. www.perldoc.perl.org. 12 December 2015. live. https://web.archive.org/web/20151207062445/http://perldoc.perl.org/functions/grep.html. 7 December 2015.
  17. Web site: pcregrep man page. www.pcre.org. University of Cambridge. 12 December 2015. live. https://web.archive.org/web/20151223035259/http://www.pcre.org/original/doc/html/pcregrep.html. 23 December 2015.
  18. Web site: grep(1). www.linux.die.net. 12 December 2015. live. https://web.archive.org/web/20151210004321/http://linux.die.net/man/1/grep. 10 December 2015.
  19. Book: Spalding , George . Windows 2000 administration. registration. 2010-12-10. Network professional's library. 2000. Osborne/McGraw-Hill. 978-0-07-882582-8. 634. QGREP.EXE[:] A similar tool to grep in UNIX, this tool can be used to search for a text string.
  20. Web site: MSX-DOS2 Tools User's Manual by ASCII Corporation. April 1993.
  21. Web site: IBM System i Version 7.2 Programming Qshell . en . IBM . . IBM . 2020-09-05 .
  22. Web site: Review: Adobe InDesign CS3 - CreativePro.com. 20 April 2007. creativepro.com. 24 April 2018. live. https://web.archive.org/web/20180105233709/https://creativepro.com/review-adobe-indesign-cs3/. 5 January 2018.
  23. Web site: InDesign Help: find/change. 2016-08-12. live. https://web.archive.org/web/20160828124223/https://helpx.adobe.com/indesign/using/find-change.html. 2016-08-28.
  24. Web site: InDesign: GREP Styles (1) Setting text between parentheses in Italic . 2018-01-05 . live . https://web.archive.org/web/20170924230421/http://carijansen.com/tip-088/ . 2017-09-24 .
  25. Web site: InDesign Help: GREP styles. 2016-08-12. live. https://web.archive.org/web/20160828114627/https://helpx.adobe.com/indesign/using/drop-caps-nested-styles.html#create_grep_styles. 2016-08-28.
  26. Sun Expert. S. Lee Henry . June 1998 . 35–26. Proper Searching.
  27. Web site: New words list December 2003. 2021-12-06. Oxford English Dictionary.
  28. Jargon File, article "Documentation"