grep

 grep [options] PATTERN [FILE...]
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/

Linux.CommanlineTool.grep的更多相关文章

  1. 由一条Linux的grep命令说起

    今天在开发的时候,看到同事使用了这样的一条linux命令 grep 'class YourClass' -rwi * |grep -v svn 想到了 grep命令的,几个参数. -r 明确要求搜索子 ...

  2. Linux下grep显示前后几行信息

    Linux下grep显示前后几行信息 标准unix/linux下的grep通过下面參数控制上下文 grep -C 5 foo file 显示file文件里匹配foo字串那行以及上下5行grep -B ...

  3. linux 命令grep

    linux 命令grep grep命令用来搜索文本,或从给定的文件中搜索行内包含了给定字符串或单词的文件.通常来说,grep显示匹配的行.使用grep来搜索包括一个或多个正则表达式匹配到的文本行,然后 ...

  4. Linux 命令 - grep: 正则搜索文本

    grep 搜索文本文件中与指定正则表达式匹配的行 命令格式 grep [OPTIONS] PATTERN [FILE...] 命令参数 Generic Program Information --he ...

  5. linux 用grep匹配横线

    file的内容如下: ADSFADSFSDFASDFJKJK----------------------------------------ADSFADSFSDFASDFJKJK----------- ...

  6. Linux正则表达式grep与egrep

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

  7. 12个 Linux 中 grep 命令的超级用法实例

    12个 Linux 中 grep 命令的超级用法实例 你是否遇到过需要在文件中查找一个特定的字符串或者样式,但是不知道从哪儿开始?那么,就请grep来帮你吧. grep是每个Linux发行版都预装的一 ...

  8. Linux中Grep常用的15个例子【转】

    转自:https://www.aliyun.com/jiaocheng/1390860.html?spm=5176.100033.1.9.6a1e41e8Pdjynm 摘要:Grep命令主要用于从文件 ...

  9. 【linux】linux命令grep + awk 详解

    linux命令grep  +  awk 详解 grep:https://www.cnblogs.com/flyor/p/6411140.html awk:https://www.cnblogs.com ...

随机推荐

  1. 认识 Thymeleaf

  2. MONGO的简单语法,新手实用

    window上启动方式:(简单略) mongod --dbpath E:\study_lib\mongodb\db --port=27000 show dababases; (创建配置文件的启动方式) ...

  3. new usercontrol

    http://www.codeproject.com/Articles/12421/The-Grouper-A-Custom-Groupbox-Control

  4. Android studio 安装中遇到一些问题的解决办法,分享一下

    从eclipse转到android studio也是很无耐,刚开始总是会遇到很多难题,但是都不要轻言放弃. 以下是我遇到的问题,并通过搜索引擎找到的解决办法,善用工具,善用头脑,勿为伸手之人. And ...

  5. 排列组合或容斥原理 SPOJ - AMR11H

    题目链接: https://vjudge.net/contest/237052#problem/H 这里给你一串数字,让你计算同时拥有这串数字最大值和最小值的子集(连续)和子序列(可以不连续)的数量, ...

  6. Python+Selenium学习--操作测试对象

    场景 前面已经讲解了如果定位对象,定位之后需要对这个对象进行操作.是鼠标点击还是键盘输入,取决于我们定位的对象缩支持的操作. webdriver中比较常用的操作元素的方法有下面几个: clear    ...

  7. Sangfor_AC用户不在线但在“在线用户管理”里有显示

    现象:用户实际不在线,但是在“在线用户管理”里有看到,而且在线时间很长. 分析:用户通过IP上线以后,只要IP地址在线或者下线时间不超过“无流量自动注销的时间”,那么就会显示到“在线用户管理”里. 而 ...

  8. Inno Setup安装程序单例运行

    1.源起: KV项目下载底层升级包,老是报出升级文件占用问题,反复分析,不得其所. 今天突然发现同时启动多个升级程序实例,分析认为安装包同时被调用多次,引发实例访问文件冲突,导致此问题. 安装程序由I ...

  9. Extract Dataset

    FROM <SAP PROGRAM DESIGN> Extract Datasets简称为Extract,是SAP中除了内表之外的另一种大量内存数据处理方式,允许用户动态地存储并排序结构化 ...

  10. coding利用Webhook实现Push代码后的jenkins自动构建

    安装jenkins 篇:http://www.cnblogs.com/loveyouyou616/p/8714544.html 之前部署了持续集成工具jenkins.通常是开发后的代码先推到 远程代码 ...