分享 [技术分享] 关于 grep,一个有 40 年历史的 Unix 命令

gitcafe · 2015年01月13日 · 最后由 gitcafe 回复于 2015年01月21日 · 3016 次阅读

"在我公开 grep 这个命令之前的很长一段时间里它都是我的私有命令。"

  • Ken Thompson

关于 grep,一个有 40 年历史的 Unix 命令

grep 是一个在 Unix 上的命令行工具(大多数 Unix 工具都是在命令行里的),它通过给定的模式来搜索一个输入文件,并输出匹配这些模式的那些文本。如果你正在读这篇文章,你大概对它并不陌生。

grep 是 Ken Thompson 写的,他也是 Unix 的创造者。grep 最先出现在 Unix v4 中,和今天的 grep 相比少了一些功能。

这是 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 的条目(直到我编辑修改之前)。

Unixv4 上 grep 的 man 手册:

.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 手册的链接。并且在第二天我收到了以下这封回信:

From: Ken Thompson [email protected] Date: Sun,Jan 12,2014 at 8:26PM Subject: Re: the birthday of grep To: benjamin [email protected]

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 [email protected] 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.

grep 的发明和背后的含义

一个叫 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

The Jargon File

我从没有使用过 ed,它是 Ken Thompson 写的一个行编辑器,除了尝试这个功能:

$ed /etc/passwd 699 g/bash/p root🇽0:0:root:/root:/bin/bash brm🇽1000💯:/home/brm:/bin/bash git🇽619:619:git daemon user:/:/bin/bash $

fgrep,egrep 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 手册中。

GNU grep 如果你在使用 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 答复说 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 手册中。

GNU grep 如果你在使用 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原文连接

http://lists.freebsd.org/pipermail/freebsd-current/2010-August/019310.html "The key to making programs fast is to make them do practically nothing."

GNU grep 这一段重复了

难怪 Mac 上的 grep 和 linux 下不一样,害得我好多 scripts 不能正确运行

#2 楼 @johny Ooooops ! 感谢指正,今后我们会多加注意 XD

需要 登录 后方可回复, 如果你还没有账号请 注册新账号