ArticleslgStudy

science

Grep

Grep is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Grep rather than just read about it. In short: 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. grep was originally developed for the Unix operating system, and is commonly available on Unix-like and some other systems such as OS-9.

Grep — main illustration
Grep — illustration

Key takeaways

  • Grep belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Grep to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Grep from memory before moving on to harder problems.

Reference excerpt

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. grep was originally developed for the Unix operating system, and is commonly available on Unix-like and some other systems such as OS-9. The shell command that runs the utility has the same name: grep.

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. 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. 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. 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. 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. 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 or where in other languages. The 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 command. 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

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 Unix. It was ported to OS/2, MS-DOS, and Windows. agrep matches even when the text 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).

See also

agrep – an approximate string-matching command Boyer–Moore string-search algorithm – search algorithm used in grep find (Unix) – a Unix command that finds files instead of text find (Windows) or findstr – a DOS/Windows command that searches for text in files, similarly to grep List of POSIX commands ngrep – the network grep vgrep – a humorous term for visual inspection

References

Notes

External links

GNU Grep official website GNU Grep manual grep(1) – Plan 9 Programmer's Manual, Volume 1 grep(1) – Inferno General commands Manual "why GNU grep is fast" - implementation details from GNU grep's author. Command Grep – 25 practical examples

Worked examples

Example 1 — a first encounter with Grep

Start with the simplest possible case. Write down what Grep claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Grep before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Grep ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Grep

In research
Grep appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Grep in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Grep is common in secondary-school and first-year university syllabi. It links to neighbouring topics IBM i Qshell commands, Inferno (operating system) commands, Plan 9 commands, so understanding it makes those chapters shorter.
In everyday life
Look for Grep outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Grep in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Grep means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Grep out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Grep in simple terms?

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. grep was originally developed for the Unix operating system, and is commonly available on Unix-like…

Why does Grep matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Grep?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Grep.

Tags

  • IBM i Qshell commands
  • Inferno (operating system) commands
  • Plan 9 commands
  • Standard Unix programs
  • Unix SUS2008 utilities
  • Unix text processing utilities

Keep exploring