Linux.CommanlineTool.grep

grep

1 grep [options] PATTERN [FILE...]
2 grep [options] [-e PATTERN | -f FILE] [FILE...]

1. grep的Regular Expression

Ref[1]

A): 匹配单字符

reg ex中基本的构建块是匹配单个字符的reg ex。大多数字符(包括数字和字母)都是匹配自己的reg ex。

B): 元字符

任何有特殊含义的元字符(metacharacter)都是被前导的反斜线(backslash, 即: \)开始标识。

C): bracket expression

bracket expression是由[]包围的字符列表。这类表达式匹配列表中的任何一个字符(注意是 只匹配一个字符)。

如果^是字符列表中的第一个字符,则该表达式匹配任意一个不在列表中的字符。

例如:[0123456789] 匹配任意一个单一数字; [a-z] 匹配任意一个小写字母。

要匹配字符‘]‘, 则需要将‘]‘放在字符列表的第一个位置。例如: []a-z]

类似的要匹配‘^‘需要将‘^‘放在字符列表中非第一的位置。例如: [a^]

类似的要匹配‘-‘需要将‘-‘放在字符列表中的最后的位置。 例如: [a-]

D): 命名的字符类别 (named classes of characters)

[:alnum:]  [:alpha:] [:cntrl:] [:digit:] [:graph:] [:lower:] [:print:] [:punct:] [:space:] [:upper:] [:xdigit:]

E): ‘.‘ 圆点匹配任意一个单一字符。 \w 和 [[:alnum:]] 同义, \W 和 [^[:alnum:]] 同义。

F): ^ 和 $ 分别匹配一行的开始和结尾的空字符串。例如:

$ grep ^line1 data

匹配文件data中以"line1"开头的行。

G): \< \>

The symbols \< and \> respectively match the empty string at the beginning and end of a word.

H): \b \B

The symbol \b matches the empty string at the edge of a word, and \B matches the empty string

provided it‘s not at the edge of a word.

I): 重复操作符 (repetition operator)

repetition operator
?
The preceding item is optional and matched at most once.

*
The preceding item will be matched zero or more times.

+
The preceding item will be matched one or more times.

{n}
The preceding item is matched exactly n times.

{n,}
The preceding item is matched n or more times.

{n,m}
The preceding item is matched at least n times, but not more than m times.

J): 匹配元字符 *

?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \)

K): reg ex的操作: concatenation, join

Two regular expressions may be concatenated; the resulting regular expression matches any string formed

by concatenating two substrings that respectively match the concatenated subexpressions.

Two regular expressions may be joined by the infix operator |; the resulting regular expression matches

any string matching either subex- pression.

Repetition takes precedence over concatenation, which in turn takes precedence over alternation.

A whole subexpression may be enclosed in parentheses to override these precedence rules.

The backreference \n, where n is a single digit, matches the substring previously matched by the nth parenthesized

subexpression of the regu- lar expression.

例如: $ grep "line1\|line2" data

匹配包含"line1"或者"line2"的行。

2. grep的Example

[Todo]: Ref[2]


Reference

1. grep

http://unixhelp.ed.ac.uk/CGI/man-cgi?grep

2. Advanced Regular Expressions in Grep Command with 10 Examples – Part II

http://www.thegeekstuff.com/2011/01/advanced-regular-expressions-in-grep-command-with-10-examples-%E2%80%93-part-ii/

时间: 2024-08-30 16:29:11

Linux.CommanlineTool.grep的相关文章

linux的grep命令

grep是一种强大的文本搜索工具,它能够使用正则表达式搜索文本,并将搜索到的内容打印出来. grep命令的格式:grep [options] PATTERN [FILE..] 搜索成功将执行状态为0,未搜索到将返回1,搜索文件的路径错误将返回2. [options]: -c:显示匹配到了多少行 -n:为输出的文件添加行号 -v:显示没匹配到的行 -o:只显示被匹配到的字符串 -i:忽略大小写 --color: 给匹配到的字符添加颜色 [PATTERN]: PATERN可以使普通的字符串,也可以使

Linux正则表达式grep与egrep

Linux正则表达式grep与egrep 正则表达式:它是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.在很多文本编辑器或其他工具里,正则表达式通常被用来检索或替换那些符合某个模式的文本内容.其实正则表达式,只是一种思想,一种表示方法.只要我们使用的工具支持表示这种思想那么这个工具就可以处理正则表达式的字符串.常用的工具有grep, sed, awk,这三个都是针对文本的行才操作的. grep  过滤器语法: grep  [-cinvABC]  'word'  filenam

linux下grep、egrep及相应的正则表达式和用法

     linux下grep.egrep及相应的正则表达式和用法                       一.简介      Linux上文本处理三剑客(引用自马哥教育 ):  grep, egrep, fgrep:文本搜索工具:基于"pattern"对给定文本进行搜索操作:     sed:Stream EDitor,流编辑器,行编辑工具:文本编辑工具:     awk:GNU awk,文本格式化工具:文本报告生成器:         grep (缩写来自Globally se

Linux 命令 - grep: print lines matching a pattern

grep 搜索文本文件中与指定正则表达式匹配的行 命令格式 grep [OPTIONS] PATTERN [FILE...] 命令参数 Generic Program Information --help 打印帮助信息 -V, --version 打印版本信息 Matcher Selection -E, --extended-regexp 使用基本正则表达式(ERE)解释 PATTERN -F, --fixed-strings 每个模式作为一组固定字符串对待(以新行分隔),而不作为正则表达式.

[Linux 006]——grep和正则表达式

在使用系统时,我们或多或少的有一些搜索.查找的需求,必须要在文本中搜索某个关键字,或者过滤出文本中某些特定的行.grep 命令就为我们提供了这样一个功能,同时,grep 还可以使用正则表达式进行匹配,这是一个强大的功能,有必要好好掌握. 1.grep 初体验 grep PATTERN [OPTIONS] FILE:在文件中按照模式进行查找.FILE 是我们要查找的目标文件,如果不指定目标文件,grep 将会从标准输入中读取输入的内容,然后进行匹配.为了方便起见,本文的所有演示都在命令行中通过标准

Linux中grep、egrep正则表达式的使用

正则表达式的由来 正则表达式,英文写法Regular Expression,在编程语言中常被简写为regex.regexp等.它是用来描述.匹配一系列符合某个句法规则字符串的单个字符串. 正则表达式通常被用来检索.替换那些符合某个模式(Pattern)的文本. 1950年代,UNIX之父Ken Thompson将正则表达式引入编辑器QED,然后是编辑器ed,最终引入到grep中.从此,正则表达式被广泛地应用到了各种UNIX或类UNIX系统的工具之中,例如perl. 近些年来,主流操作系统.主流开

Linux 命令grep, egrep,正则表达式大全

Linux grep 命令     Linux系统中grep,egrep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户. 1.grep : 最早的文本匹配程序,使用POSIX定义的基本正则表达式(BRE)来匹配文本. 2.egrep : 扩展式grep,其使用扩展式正规表达式(ERE)来匹配文本. 3.fgrep : 快速grep,这个版

linux 命令grep正则表达式

Linux 命令grep正则表达式 在linux里面有处理文本的三剑,现在我要说的就是grep正则表达式的使用,正则表达式只是一种表示法,只要工具支持这种表示法,那么该工具就可以处理正则表达式的字符串.vi grep ,awk ,sed 等都支持正则表达式..正则表达式有基本正则表达式和扩展正则表达式. grep默认就是基本表达式. 基本正则表达式: 默认匹配次数:贪婪模式,尽可能多的去匹配. 扩展正则表达式:基于基本正则表达式多加了一些功能,基本一样. 1. grep命令 grep [参数]

linux中grep和find的用法区别

linux中grep和find的用法区别 本文章详细的介绍了关于在linux中的grep和find两个命令的用法介绍,以及后面总结了它们两年用法区别哦. 先我们来介绍一下关于grep用法和一些小注意事项 使用过程中,使用最多的参数就是 -v ,但是用着并不爽. 比如说,我想查找一个单词“UserService”,但是像”*.svn” 这种文件就不用显示了,我该怎么做呢? 代码如下 复制代码 grep -r "UserService" ./ | grep -v "svn&quo