The ubiquitous command 'grep' came from a four-character text editor routine
The standard Unix search command grep was written by Ken Thompson in 1973. Its name originates from a command sequence in ed, an early Unix text editor: g/re/p. This string stood for "Global Regular Expression Print," instructing the editor to search an entire file globally for lines matching a specified regular expression and print them to the terminal.
An Overnight Tool for a Historical Mystery
In 1973 at Bell Laboratories, computer scientist Lee E. McMahon was investigating a classic historical puzzle: the disputed authorship of several essays in The Federalist Papers. McMahon needed a fast, automated way to scan extensive text files for distinctive linguistic patterns, phrases, and word frequencies across candidate authors. At the time, performing text searches in the nascent Unix operating system required loading a document into the interactive text editor ed, executing a pattern-matching command, and manually reviewing the buffer.
Recognizing that McMahon needed a dedicated tool that could scan files from the shell without the overhead of an interactive editor session, Ken Thompson adapted the search functionality into a standalone program. Thompson extracted the regular expression matching code from ed, packaged it into an independent command-line utility, and presented it to McMahon. What began as an overnight solution to a specific textual analysis problem quickly became one of the foundational utilities in software engineering.
Deconstructing the Syntax: g/re/p
The name of the new tool was directly derived from the exact command syntax used within ed: g/re/p. Because ed was a line-oriented editor developed for memory-constrained systems without graphical displays or full-screen interfaces, all operations were controlled via compact keystroke sequences. Understanding this cryptic sequence explains the fundamental logic of Unix pattern matching.
In ed syntax, the initial character 'g' represents a global prefix, instructing the editor to apply an action across every line in the entire file rather than defaulting to the current line. The surrounding slashes '/.../' serve as delimiters enclosing a regular expression ('re'), defining the pattern to be matched. Finally, the trailing character 'p' is the print command, directing the editor to output any line satisfying the search condition. Thus, the sequence g/re/p literally translated to 'globally search for a regular expression and print matching lines.'