Differences between grep, pgrep, egrep, and fgrep (Linux):

grep

grep is an acronym that stands for "Global Regular Expressions Print". grep is a program which scans a specified file or files line by line, returning lines that contain a pattern. A pattern is an expression that specifies a set of strings by interpreting characters as meta-characters. For example the asterisk meta character (*) is interpreted as meaning "zero or more of the preceding element". This enables users to type a short series of characters and meta characters into a grep command to have the computer show us what lines in which files match.

The standard grep command looks like:

grep <flags> '<regular expression>' <filename>

grep prints the search results to the screen (stdout) and returns the following exit values:

0    A match was found.
1 No match was found.
>1 A syntax error was found or a file was inaccessible
(even if matches were found).

Some common flags are: -c for counting the number of successful matches and not printing the actual matches, -i to make the search case insensitive, -n to print the line number before each match printout, -v to take the complement of the regular expression (i.e. return the lines which don't match), and -l to print the file names of files with lines which match the expression.

egrep

egrep is an acronym that stands for "Extended Global Regular Expressions Print".

The 'E' in egrep means treat the pattern as a regular expression. "Extended Regular Expressions" abbreviated 'ERE' is enabled in egrep. egrep (which is the same as grep -E) treats +?|(, and ) as meta-characters.

In basic regular expressions (with grep), the meta-characters ?+{|(, and ) lose their special meaning. If you want grep to treat these characters as meta-characters, escape them \?\+\{\|\(, and \).

For example, here grep uses basic regular expressions where the plus is treated literally, any line with a plus in it is returned.

grep "+" myfile.txt

egrep on the other hand treats the "+" as a meta character and returns every line because plus is interpreted as "one or more times".

egrep "+" myfile.txt

Here every line is returned because the + was treated by egrep as a meta character. normal grep would have searched only for lines with a literal +.

fgrep

fgrep is an acronym that stands for "Fixed-string Global Regular Expressions Print".

fgrep (which is the same as grep -F) is fixed or fast grep and behaves as grep but does NOT recognize any regular expression meta-characters as being special. The search will complete faster because it only processes a simple string rather than a complex pattern.

For example, if I wanted to search my .bash_profile for a literal dot (.) then using grep would be difficult because I would have to escape the dot because dot is a meta character that means 'wild-card, any single character':

grep "." myfile.txt

The above command returns every line of myfile.txt. Do this instead:

fgrep "." myfile.txt

Then only the lines that have a literal '.' in them are returned. fgrep helps us not bother escaping our meta characters.

pgrep

pgrep is an acronym that stands for "Process-ID Global Regular Expressions Print".

pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout. pgrep is handy when all you want to know is the process id integer of a process. For example, if I wanted to know only the process ID of my mysql process I would use the command pgrep mysql which would return a process ID like 7312.

来自:http://superuser.com/questions/508881/what-is-the-difference-between-grep-pgrep-egrep-fgrep

grep/pgrep/egrep/fgrep的更多相关文章

  1. grep, egrep, fgrep笔记

    grep, egrep, fgrep grep: 根据模式搜索文本,并将符合模式的文本行显示出来.Pattern: 文本字符和正则表达式的元字符组合而成匹配条件 grep [options] PATT ...

  2. grep egrep fgrep命令

    一.grep.egrep.fgrep命令 本文中主要介绍了linux系统下grep egrep fgrep命令和正则表达式的基本参数和使用格式.方法.(注释:文中fg代表例子,) 1.1.基本定义: ...

  3. grep与正则表达式,grep、egrep和fgrep

    grep用法详解:grep与正则表达式 首先要记住的是: 正则表达式与通配符不一样,它们表示的含义并不相同!正则表达式只是一种表示法,只要工具支持这种表示法, 那么该工具就可以处理正则表达式的字符串. ...

  4. grep、egrep、fgrep的用法与特性详解

    [转载自]http://tanxw.blog.51cto.com/4309543/1361993 开篇        学习Linux也有一段时间了,对Linux多少也算是有点了解了,越是了解也就越对这 ...

  5. Linux三剑客之grep 与 egrep

    grep: Linux上文本处理三剑客 grep:文本过滤(模式:pattern)工具; *(grep, egrep, fgrep) sed:stream editor,文本编辑工具: awk:Lin ...

  6. linux中grep和egrep的用法

    1. grep简介 grep (global search regular expression_r(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大 ...

  7. Linux正则表达式grep与egrep

    grep -io "http:/=[A-Z0-9]\{16\}" ./wsxf.txt >wsxf_urls.txt Linux正则表达式grep与egrep 正则表达式:它 ...

  8. grep和egrep正则表达式

    Linux上文本处理三剑客 grep :文本过滤( 模式:pattern) 工具 grep, egrep, fgrep (不支持正则表达式搜索,但搜索纯文本的数据最快) sed :stream edi ...

  9. grep、egrep命令用法

    何谓正则表达式 正则表达式,又称正规表示法.常规表示法(Regular Expression,在代码中常简写为regex.regexp或RE),是一类字符所书写的模式,其中许多字符不表示其字面意义,而 ...

随机推荐

  1. better-scroll插件

    参考网址:慕课网  http://coding.imooc.com/lesson/74.html#mid=1640 这个详细:https://zhuanlan.zhihu.com/p/25369923 ...

  2. C#面向对象(封装)

    以上就是面向对象的封装和初始化:

  3. ssh-keygen -t rsa -b 4096 -C "邮箱"

    ssh-keygen -t rsa -b 4096 -C "邮箱":这条命令的目的是为了让本地机器ssh登录远程机器上的GitHub账户无需输入密码.将这条命令分解: 1.ssh- ...

  4. window 命令大全

    运行操作 CMD命令:开始->运行->键入cmd或command(在命令行里可以看到系统版本.文件系统版本) CMD命令锦集       1. gpedit.msc-----组策略 2. ...

  5. 求next数组的两种方法

    法一解释:转自http://www.cnblogs.com/yjiyjige/p/3263858.html 关键运算步骤 i 0 1 2 3 4 5 6 7 8 9 A B A C D A B A B ...

  6. windows系统下ruby开发环境的搭建方法

    ruby是一种简单快捷的面向对象的脚本语言,非常直观.下面说一下windows系统下ruby开发环境的搭建方法. 工具/原料 rubyinstaller.exe 方法/步骤 1 到如下网站去下载最新的 ...

  7. 【Java】 剑指offer(12) 机器人的运动范围

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 地上有一个m行n列的方格.一个机器人从坐标(0, 0)的格子开始移 ...

  8. Jetbrains 2018 等系列软件激活破解除去黄色警告框方法(含多个平台)

    现在以IDEA 2018软件在windows为例进行讲解破解方法,其它软件及其在不同平台的破解方法原理 大同小异,有需要的有兴趣的可以尝试一下. 最近更新了Intellij IDEA到2018.1.5 ...

  9. vscode下Python设置参考

    用于VS代码的Python扩展是高度可配置的.此页面介绍了可以使用的关键设置. 请参阅用户和工作区设置,以了解有关在VS代码中使用设置的更多信息. 常规设置 设置 默认 描述 python.pytho ...

  10. POJ 1279 Art Gallery【半平面交】(求多边形的核)(模板题)

    <题目链接> 题目大意: 按顺时针顺序给出一个N边形,求N边形的核的面积. (多边形的核:它是平面简单多边形的核是该多边形内部的一个点集该点集中任意一点与多边形边界上一点的连线都处于这个多 ...