"在我公開 grep 這個命令以前的很長一段時間裏它都是個人私有命令。"
- Ken Thompsonhtml
grep 是一個在 Unix 上的命令行工具(大多數 Unix 工具都是在命令行裏的),它經過給定的模式來搜索一個輸入文件,並輸出匹配這些模式的那些文本。若是你正在讀這篇文章,你大概對它並不陌生。linux
grep 是 Ken Thompson 寫的,他也是 Unix 的創造者。grep 最早出如今 Unix v4 中,和今天的 grep 相比少了一些功能。git
這是 Unix v6 中的 grep 的 man 信息。正則表達式
NAME grep - search a file for a pattern SYNOPSIS grep [ -v ] [ -b ] [ -c ] [ -n ] exression [ file ] ... DESCRIPTION Grep searches the input files (standard input default) for lines matching the regular expression. Normally, each line found is copied to the standard output. If the -v flag is used, only a count of matching lines is printed. If the -c flag is used, only a count of matching lines is printed. If the -n flag is used, each line is preceded its relative line number in file. If the -b flag is used, each line is preceded by the block number on which it was found. This is sometimes useful in locating disk block numbers by context. In all cases the file name is shown if there is more than one input file. For a complete description of the regular expression, see ed (I). Care should be taken when using the characters $ * [^ | () and \ in the regular expression as they are also meaningful to the Shell. It is generally necessary to enclose the entire expression argument in quotes. SEE ALSO ed (I),sh (I) BUGS Liners are limited to 256 characters;longer lines are truncated
可能由於在 Unixv4 中 grep 的 man 手冊上寫有日期 3/3/1973,許多因特網上的文章都假定這是 grep 被創造的時間,包括維基百科上 grep 的條目(直到我編輯修改以前)。express
Unixv4 上 grep 的 man 手冊:bash
.th GREP I 3/3/73 .sh NAME grep \*- search a file for a pattern
因爲缺乏維基百科上關於創造日期的貢獻和我並無經過 Google 搜索到 Unix v4 的 man 手冊,我抱着很小的但願轉向了 Reddit 而且給 Ken Thompson 寫了郵件。在 Reddit 上,一些人提供了 Unix v4 man 手冊的連接。而且在次日我收到了如下這封回信:app
From: Ken Thompson <ken@google.com> Date: Sun,Jan 12,2014 at 8:26PM Subject: Re: the birthday of grep To: benjamin <benjaminrtz@gmail.com> I don‘t remember what the date in .th macro means. It probably is the last modified date of the man page,not the command. I never recall that we ever noted when a command was created. Normally,we would only be able to say something like "it first appeared in version X of the manual. Even after saying that,we were very careful not to put junk into the utilities directory.Grep was a private command of mine for quite a while before i made it public. Thats the long answer.the short answer is "sometime before the 4th edition." ken On Sat ,Jan 11,2014 at 1:14PM,benjamin <benjaminrtz@gmail.com> wrote: Ken, Can you please confirm the birthday of grep? Grep was created by Ken Thompson as a standalone application adapted from the regular expression parser he had written for ed(which he also created).[3]In ed,the command g/re/p would print all lines matching a previously defined pattern.[4][5]Grep's official creation date is given as March 3,1973,in the Manual for Unix Version 4.[citation needed]http://en.wikipedia.org/wiki/Grep - benjamin rualthanzauva
我欣喜若狂的跑向個人妻子並向她展現郵件,儘管她幾乎不可能明白這種興奮。感謝 Ken 和 /r/linux.編輯器
一個叫 McIlroy 的人聲稱 grep 是爲他發明的。在一個輕筆記上寫着,我不是很肯定。工具
「一個下午我問 Ken Thompson 他是否能夠把正則表達式從編輯器中抽離出來而且作成一個簡單易用的程序。他說能夠。次日早上我在郵件裏發現了一個 note 而且附帶一個叫作 grep 的程序。它像一個小咒語。當我詢問這個有趣的名字有什麼含義,Ken 回答說那很明顯。它表明着它模擬的編輯功能,g/re/p(global regular expression print)」性能
Chapter 9,On the Early History and Impact of Unix Tools to Build the Tools for a New Millenium
grep 名字的起源也是被 Dennis MacAlistair Ritchie(1941 年 9 月 9 日-2011 年 10 月 12 日)確認的,他是 C 語言之父。若是你曾經記得的話,Dennis 是在 Jobs 去世後一週也離開了人世,但不多有人知道他。
It has been alleged that the source is from the title of a paper "A General Regular Expression Parser",but dmr confirms the g/re/p etymology ---ESR
我從沒有使用過 ed,它是 Ken Thompson 寫的一個行編輯器,除了嘗試這個功能:
$ed /etc/passwd 699 g/bash/p root:x:0:0:root:/root:/bin/bash brm:x:1000:100::/home/brm:/bin/bash git:x:619:619:git daemon user:/:/bin/bash $
fgrep 和 egrep 最初是做爲獨立的程序存在,由於他們是另一些做者寫的命令。由於歷史緣由,grep 和 egrep 發展的很是快。他們最終被合併進了同一個命令。今天 fgrep 和 egrep 爲了能使遺留的腳本能正常運行因此有指向。今天正確的使用方法是 grep -F 和 grep -E 。 -E 和 -F 選項是針對 POSIX 的。POSIX 定義了一套標準來兼容 Unix 和其餘操做系統。
Al Aho 也是一個貝爾實驗室的研究員而且是 AWK 的聯合做者,他在 1975 年的一個週末寫了 egrep 和 fgrep。
在一開始 Ken Thompson 寫的 grep 這個搜索工具時,它從文本中選擇而且打印出和模式匹配的文字。在 1975 年,就在第 6 版 Unix 發佈的時候,AlAho 決定把理論應用於實際,而且實現了完整的正則表達式(包括變化和分組這些當年 grep 中沒有的特色),他也在週末寫了 egrep。Fgrep 也是在那個週末寫的,用來處理多文本匹配。Egrep 在單個字符上的搜索功能比 grep 快 2 倍但在複雜文本上卻要慢。(主要花費了大量時間在創建狀態機用來識別模式。)
從那以後,每個工具都不按期的提高性能,大部分的像 grep 做者之間的 acfriendly rivalry(Thompson,和以後的 McMahon)和 egrep(Aho)
Grep Wars:The Strategic Search Initiative,Andrew Hume,1987
egrep 和 fgrep 在 Unix v8 的 grep 的 man 手冊中。
若是你在使用 Linux,那你正在使用 GNU grep。 除非你本身安裝了其餘 grep。在 ESXi 終端中,你有一個受限制的 grep 版本。在 Mac 上,那是 BSD grep。 你能夠經過 grep -V 來查看本身使用的是什麼版本。其餘平臺上也都是本身的 grep 版本。我建議你能夠本身編譯 GNU grep。
GNU grep 是 Mike Haerkal 寫的。 版本一試在 1988 年發出。可是它是在 1987 年 Jan 在 GNU’s Bulletin 中的文章 「The GNU'ls','grep','make' and 'ld' are in regular use」 裏做爲第二條發出的。
我問過 Mike 和 RMS 可是他們都不記得細節了。Mike 說 1988 年的夏天可是不記得具體的日期了,多是在 6 月後期和 7 月前期。RMS 答覆說
I don't remember that sort of detail after 27 years. If we said it in the bulletin ,it must be true.
可能 GNU grep 在 Mike 拿到以前已經存在了。Mike Haerkal 也是 AMD-V 的合做者,AMD 開發的 x86 CPU 的虛擬化擴展技術。
若是你對 grep 的歷史有興趣並願意分享,我會很樂意而且加進這篇文章。
感謝你的閱讀。
本文由 GitCafe 翻譯,原文做者爲 Benjamin Rualthanzauva。
原文鏈接