egrep和grep之间的区别 [英] Difference between egrep and grep

查看:104
本文介绍了egrep和grep之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 unix grep egrep 的问题C>。

由于我最近正在研究 unix shell命令 >,我想知道grep和egrep有什么区别。



我发现我们可以在正则表达式中使用grep和egrep。然而,我发现这两个命令之间有些不同:



对于 ^ ,grep和egrep具有相同的含义

然而,对于 | ,grep,没有背面斜线在 | 前面,表示 | 的字符,如果我在它前面放置反斜杠,打开它找到该行的特殊含义包含前面和后面的内容。但是,对egrep来说,情况正好相反。 | 具有grep的后面含义, \ | 在使用时具有grep的前一个含义。



有人可以解释为什么吗?

是的,我可以。

/ p>
$ b egrep 命令是 grep 二进制的快捷方式,但有一个例外,当 grep 被调用为 egrep grep 二元激活其内部逻辑,因为它将被称为 grep -E



区别在于 -E 选项可以使用扩展的正则表达式模式。它允许你使用 + | 。它们不是像我们曾经用过的单词或文件名那样的序号字符,而是 grep 二进制本身的控制命令。因此, | 表示逻辑OR。



例如,您想列出目录中的文件并仅查看那些包含mp4或avi扩展名的文件。有了 egrep ,你就可以做到:

ls | egrepmp4 | avi



在这个例子中, | 将抓取从 ls 包含mp4或avi字符串的所有名称输出。如果你用普通的 grep 命令运行它,你将不会得到任何东西,因为 grep 不知道 | 命令,它将搜索mp4 | avi整个文本字符串(带有 pipe 符号)。例如。如果你的目录中有 | MPG | cool-guy.q2.stats 文件,你可以用普通的 grep 用管道搜索。

所以,这就是为什么你应该在 egrep | $ c>命令来实现与 grep 中相同的效果。 Escaping将屏蔽 | 命令对 grep 二进制文件的特殊含义。


I have a question about grep and egrep in unix.

As I am recently studying the shell commands in unix, I want to know what are the differences between grep and egrep.

I find we can use grep and egrep with regular expression. However, I found something different between these two commands:

For ^, grep and egrep have the same meaning which is finding the lines contain the given stuffs at the very beginning.

However, for |, grep, without back slash in front of |, means character of |, and if I put a back slash in front of it, it turns on it special meaning of finding the line contains either the stuff in front of it and behind it. BUT, for egrep, it is the opposite. | has the latter meanings of grep and \| has the former meaning of grep when uses it.

Could somebody else kindly explain why?

解决方案

Yes, I could.

The egrep command is shortcut for grep binary, but with one exception, when grep is invoked as egrep the grep binary activates its internal logic as it would be called as grep -E.

The difference is that -E option enables usage of extended regexp patterns. It will allow you using of such meta-symbols as +, ? or |. They aren't ordinal characters like we used to have in words or filenames but control commands for grep binary itself. Thus, | means logical OR.

So, for example, you want to list files in directory and see only that ones which contains "mp4" or "avi" extentions. With egrep you will do:

ls | egrep "mp4|avi"

In this example | acts like OR command it will grab to output from ls all names which contain either "mp4" or "avi" strings. If you run it with plain grep command you will get nothing, because grep don't know such thing as | command and it will search for "mp4|avi" whole text string (with pipe symbol). E.g. if you have |MPG|cool-guy.q2.stats file in your dir, you will get it with plain grep searching with pipes.

So, that is why you should escape | in your egrep command to achieve the same effect as in grep. Escaping will screen off the special meaning of | command for grep binary.

这篇关于egrep和grep之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆