"在我公开 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。
原文链接