Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Fundamentals 10 min read

The Origin of grep: From ed's g/re/p to a Unix Legend

This article traces grep's 1970s Bell Labs origin from Ken Thompson's overnight creation for Doug McIlroy's dictionary search, explains how its name derives from ed's g/re/p command, compares 1973 and modern flag differences, covers egrep/fgrep evolution, and demonstrates practical log analysis using Unix pipes.

The Problem: Searching Large Text in Early Unix

When troubleshooting faults, engineers often run grep 'ERROR' app.log to filter massive log files. But why is the command called grep? The answer goes back to 1970s Bell Labs, before Linux existed.

The Dictionary That Was Too Big for ed

Early Unix used the ed line editor. Its buffer size limited searching large files. Doug McIlroy, researching text-to-speech, needed to find words with specific letter combinations (e.g., ui in fruit, guile, build) in a dictionary too large for ed. He asked Ken Thompson to extract ed 's regex matching into a standalone file scanner. The next morning, Thompson delivered grep.

Brian Kernighan also recalls Lee McMahon wanting to analyze word usage in the Federalist Papers for authorship attribution, facing the same large-text problem. Multiple Bell Labs researchers needed efficient text search.

grep Comes from an ed Command: g/re/p

In ed, the command g/re/p means: g — global (search entire buffer) re — regular expression (the pattern) p — print matching lines

Removing the slashes yields grep.

grep originates from ed command g/re/p: g = global, re = regular expression, p = print
grep originates from ed command g/re/p: g = global, re = regular expression, p = print

Example: a file words.txt with lines fruit, apple, guile, build. Inside ed:

ed -s words.txt
g/ui/p
q

Outputs fruit, guile, build. The equivalent standalone command: grep 'ui' words.txt By default, grep prints the whole matching line. Patterns can be complex regex or plain text.

1973 grep : Flags Were Different

The 4th Edition Unix manual (November 1973, man page dated March 3, 1973) already included grep. It read from files or stdin and printed matching lines. However, flags differed from today: -l printed line numbers (modern -n) -n counted matching lines (modern -c)

Line length limited to 512 characters

1973 Unix 4th edition manual vs modern GNU grep flags: -v always for non-matching lines; old -l showed line numbers, modern -l lists filenames; old -n counted hits, modern -n shows line numbers; modern -c counts hits, not in 1973 manual.
1973 Unix 4th edition manual vs modern GNU grep flags: -v always for non-matching lines; old -l showed line numbers, modern -l lists filenames; old -n counted hits, modern -n shows line numbers; modern -c counts hits, not in 1973 manual.

Modern GNU grep uses -n for line numbers and -c for counting matching lines. If a line contains ERROR three times, grep -c 'ERROR' app.log still counts that line once.

Two Siblings: egrep and fgrep

As Unix evolved, Alfred Aho developed egrep for extended regular expressions and fgrep for fast fixed-string multi-keyword matching (using the Aho–Corasick algorithm). By 7th Edition Unix, both shipped alongside grep. GNU grep merges them: -E enables extended regex (like egrep) -F treats pattern as fixed strings (like fgrep)

Example: filter lines starting with WARN or ERROR: grep -E '^(WARN|ERROR) ' app.log Search for a literal dot in a version string: printf '%s\n' v1.2 v1x2 | grep -F 'v1.2' Outputs only v1.2. For new scripts, grep -E and grep -F are recommended.

Pipes Make grep Powerful

Unix pipes ( |) let grep consume stdout from other commands, enabling composable text processing.

Concrete example: file grep-demo.log contains:

INFO start
WARN slow
ERROR disk full
INFO retry
ERROR disk full
ERROR timeout

Pipeline to count distinct errors by frequency:

grep '^ERROR ' grep-demo.log |
  sort | uniq -c | sort -nr
GNU grep example: grep -n extracts lines 3, 5, 6; sort and uniq -c pipeline shows ERROR disk full 2 times, ERROR timeout 1 time.
GNU grep example: grep -n extracts lines 3, 5, 6; sort and uniq -c pipeline shows ERROR disk full 2 times, ERROR timeout 1 time.

Result:

2 ERROR disk full
1 ERROR timeout
grep

filters, sort orders, uniq -c merges and counts. For timestamped logs, extract the relevant field before counting.

Why grep Survived Half a Century

grep

's success influenced later Unix tools like sed (stream editor). The Unix philosophy — each program does one thing well, connected via uniform I/O — remains central. Next time you type grep, remember the ancient g/re/p and the Bell Labs researchers who solved large-text search overnight.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Command Lineregular expressionslog analysistext processinggrepUnix philosophyUnix historyBell Labs

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion