http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html部分摘录于此

grep与正规表达式

 字符类

字符类的搜索:如果我想要搜寻 test 或 taste 这两个单字时,可以发现到,其实她们有共通的 't?st' 存在~这个时候,我可以这样来搜寻:

  1. [root@www ~]# grep -n 't[ae]st' regular_express.txt
  2. 8:I can't finish the test.
  3. 9:Oh! The soup taste good.

其实 [] 里面不论有几个字节,他都谨代表某『一个』字节, 所以,上面的例子说明了,我需要的字串是『tast』或『test』两个字串而已!

字符类的反向选择 [^] :如果想要搜索到有 oo 的行,但不想要 oo 前面有 g,如下

  1. [root@www ~]# grep -n '[^g]oo' regular_express.txt
  2. 2:apple is my favorite food.
  3. 3:Football game is not use feet only.
  4. 18:google is the best tools for search keyword.
  5. 19:goooooogle yes!

第 2,3 行没有疑问,因为 foo 与 Foo 均可被接受!

但是第 18 行明明有 google 的 goo 啊~别忘记了,因为该行后面出现了 tool 的 too 啊!所以该行也被列出来~ 也就是说, 18 行里面虽然出现了我们所不要的项目 (goo) 但是由於有需要的项目 (too) , 因此,是符合字串搜寻的喔!

至於第 19 行,同样的,因为 goooooogle 里面的 oo 前面可能是 o ,例如: go(ooo)oogle ,所以,这一行也是符合需求的!

字符类的连续:再来,假设我 oo 前面不想要有小写字节,所以,我可以这样写 [^abcd....z]oo , 但是这样似乎不怎么方便,由於小写字节的 ASCII 上编码的顺序是连续的, 因此,我们可以将之简化为底下这样:

  1. [root@www ~]# grep -n '[^a-z]oo' regular_express.txt
  2. 3:Football game is not use feet only.

也就是说,当我们在一组集合字节中,如果该字节组是连续的,例如大写英文/小写英文/数字等等, 就可以使用[a-z],[A-Z],[0-9]等方式来书写,那么如果我们的要求字串是数字与英文呢? 呵呵!就将他全部写在一起,变成:[a-zA-Z0-9]。

我们要取得有数字的那一行,就这样:

  1. [root@www ~]# grep -n '[0-9]' regular_express.txt
  2. 5:However, this dress is about $ 3183 dollars.
  3. 15:You are the best is mean you are the no. 1.

行首与行尾字节 ^ $
行首字符:如果我想要让 the 只在行首列出呢? 这个时候就得要使用定位字节了!我们可以这样做:

  1. [root@www ~]# grep -n '^the' regular_express.txt
  2. 12:the symbol '*' is represented as start.

此时,就只剩下第 12 行,因为只有第 12 行的行首是 the 开头啊~此外, 如果我想要开头是小写字节的那一行就列出呢?可以这样:

  1. [root@www ~]# grep -n '^[a-z]' regular_express.txt
  2. 2:apple is my favorite food.
  3. 4:this dress doesn't fit me.
  4. 10:motorcycle is cheap than car.
  5. 12:the symbol '*' is represented as start.
  6. 18:google is the best tools for search keyword.
  7. 19:goooooogle yes!
  8. 20:go! go! Let's go.

如果我不想要开头是英文字母,则可以是这样:

  1. [root@www ~]# grep -n '^[^a-zA-Z]' regular_express.txt
  2. 1:"Open Source" is a good mechanism to develop programs.
  3. 21:# I am VBird

^ 符号,在字符类符号(括号[])之内与之外是不同的! 在 [] 内代表『反向选择』,在 [] 之外则代表定位在行首的意义!

那如果我想要找出来,行尾结束为小数点 (.) 的那一行:

  1. [root@www ~]# grep -n '\.$' regular_express.txt
  2. 1:"Open Source" is a good mechanism to develop programs.
  3. 2:apple is my favorite food.
  4. 3:Football game is not use feet only.
  5. 4:this dress doesn't fit me.
  6. 10:motorcycle is cheap than car.
  7. 11:This window is clear.
  8. 12:the symbol '*' is represented as start.
  9. 15:You are the best is mean you are the no. 1.
  10. 16:The world <Happy> is the same with "glad".
  11. 17:I like dog.
  12. 18:google is the best tools for search keyword.
  13. 20:go! go! Let's go.

特别注意到,因为小数点具有其他意义(底下会介绍),所以必须要使用转义字符(\)来加以解除其特殊意义!

找出空白行:

  1. [root@www ~]# grep -n '^$' regular_express.txt
  2. 22:

因为只有行首跟行尾 (^$),所以,这样就可以找出空白行啦!

任意一个字节 . 与重复字节 *
这两个符号在正则表达式的意义如下:

  1. . (小数点):代表『一定有一个任意字节』的意思;
  2. * (星号):代表『重复前一个字符, 0 到无穷多次』的意思,为组合形态

假设我需要找出 g??d 的字串,亦即共有四个字节, 起头是 g 而结束是 d ,我可以这样做:

  1. [root@www ~]# grep -n 'g..d' regular_express.txt
  2. 1:"Open Source" is a good mechanism to develop programs.
  3. 9:Oh! The soup taste good.
  4. 16:The world <Happy> is the same with "glad".

因为强调 g 与 d 之间一定要存在两个字节,因此,第 13 行的 god 与第 14 行的 gd 就不会被列出来啦!

如果我想要列出有 oo, ooo, oooo 等等的数据, 也就是说,至少要有两个(含) o 以上,该如何是好?

因为 * 代表的是『重复 0 个或多个前面的 RE 字符』的意义, 因此,『o*』代表的是:『拥有空字节或一个 o 以上的字节』,因此,『 grep -n 'o*' regular_express.txt 』将会把所有的数据都列印出来终端上!

当我们需要『至少两个 o 以上的字串』时,就需要 ooo* ,亦即是:

  1. [root@www ~]# grep -n 'ooo*' regular_express.txt
  2. 1:"Open Source" is a good mechanism to develop programs.
  3. 2:apple is my favorite food.
  4. 3:Football game is not use feet only.
  5. 9:Oh! The soup taste good.
  6. 18:google is the best tools for search keyword.
  7. 19:goooooogle yes!

如果我想要字串开头与结尾都是 g,但是两个 g 之间仅能存在至少一个 o ,亦即是 gog, goog, gooog.... 等等,那该如何?

  1. [root@www ~]# grep -n 'goo*g' regular_express.txt
  2. 18:google is the best tools for search keyword.
  3. 19:goooooogle yes!

如果我想要找出 g 开头与 g 结尾的行,当中的字符可有可无

  1. [root@www ~]# grep -n 'g.*g' regular_express.txt
  2. 1:"Open Source" is a good mechanism to develop programs.
  3. 14:The gd software is a library for drafting programs.
  4. 18:google is the best tools for search keyword.
  5. 19:goooooogle yes!
  6. 20:go! go! Let's go.

因为是代表 g 开头与 g 结尾,中间任意字节均可接受,所以,第 1, 14, 20 行是可接受的喔! 这个 .* 的 RE 表示任意字符是很常见的.

如果我想要找出『任意数字』的行?因为仅有数字,所以就成为:

  1. [root@www ~]# grep -n '[0-9][0-9]*' regular_express.txt
  2. 5:However, this dress is about $ 3183 dollars.
  3. 15:You are the best is mean you are the no. 1.

限定连续 RE 字符范围 {}
我们可以利用 . 与 RE 字符及 * 来配置 0 个到无限多个重复字节, 那如果我想要限制一个范围区间内的重复字节数呢?

举例来说,我想要找出两个到五个 o 的连续字串,该如何作?这时候就得要使用到限定范围的字符 {} 了。 但因为 { 与 } 的符号在 shell 是有特殊意义的,因此, 我们必须要使用字符   \ 来让他失去特殊意义才行。 至於 {} 的语法是这样的,假设我要找到两个 o 的字串,可以是:

  1. [root@www ~]# grep -n 'o\{2\}' regular_express.txt
  2. 1:"Open Source" is a good mechanism to develop programs.
  3. 2:apple is my favorite food.
  4. 3:Football game is not use feet only.
  5. 9:Oh! The soup taste good.
  6. 18:google is the best tools for search ke
  7. 19:goooooogle yes!

假设我们要找出 g 后面接 2 到 5 个 o ,然后再接一个 g 的字串,他会是这样:

  1. [root@www ~]# grep -n 'go\{2,5\}g' regular_express.txt
  2. 18:google is the best tools for search keyword.

如果我想要的是 2 个 o 以上的 goooo....g 呢?除了可以是 gooo*g ,也可以是:

  1. [root@www ~]# grep -n 'go\{2,\}g' regular_express.txt
  2. 18:google is the best tools for search keyword.
  3. 19:goooooogle yes!

grep理解的更多相关文章

  1. 【Linux】- 对find,xargs,grep和管道的一些理解

    问题 相信大家都知道在目录中搜索含有固定字符串文件的命令: find . -name '*.py' |xargs grep test 刚开始的时候,我不熟悉xargs命令,所以直接使用的命令是: fi ...

  2. grep的简单理解

    概述: grep最早由肯·汤普逊写成.原先是ed下的一个应用程序,名称来自于g/re/p(globally search a regular expression and print,以正则进行全域查 ...

  3. 彻底理解 Linux 的搜索工具: grep 和 awk

    grep 官方手册 awk 官方手册, awk 学习资料 1. grep grep 用于打印匹配指定模式的行. 1.1 介绍 grep 命令从输入文件中查找匹配到给定模式列表的行.发现匹配到的行后,默 ...

  4. 全面理解Git

    前言 人生贵知心,定交无暮早. 原文博客地址:Git命令总结 知乎专栏&&简书专题:前端进击者(知乎)  前端进击者(简书) 正文 1.Git简介 Git的诞生确实是一个有趣的故事,我 ...

  5. 理解Docker(5):Docker 网络

    本系列文章将介绍 Docker的相关知识: (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 Linux namespace 隔离容器的运行环境 ...

  6. 理解Docker(4):Docker 容器使用 cgroups 限制资源使用

    本系列文章将介绍Docker的有关知识: (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 Linux namespace 隔离容器的运行环境 ...

  7. 理解Docker(3):Docker 使用 Linux namespace 隔离容器的运行环境

    本系列文章将介绍Docker的有关知识: (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 Linux namespace 隔离容器的运行环境 ...

  8. 【转】理解inode

    From:http://www.ruanyifeng.com/blog/2011/12/inode.html  阮一峰大神真NB 作者: 阮一峰 日期: 2011年12月 4日 inode是一个重要概 ...

  9. Linux查找命令:grep,awk,sed

    grep grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具 ...

随机推荐

  1. 云服务中IaaS、PaaS、SaaS的区别

    越来越多的软件,开始采用云服务. 云服务只是一个统称,可以分成三大类. IaaS:基础设施服务,Infrastructure-as-a-service PaaS:平台服务,Platform-as-a- ...

  2. JDK原子类操作

    JDK原子类操作及原理 在JDK5之后,JDK提供了对变量的原子类操作, java.util.concurrent.atomic里都是原子类 原子类的分类 原子更新基本类型 原子更新数组 原子更新抽象 ...

  3. java中 awt Graphics2D

    Graphics2D ,Graphics 类,提供了对几何形状.坐标转换.颜色管理和文本布局更为复杂的控制.它是用于在 Java(tm) 平台上呈现二维形状.文本和图像的基础类.验证码生成可以用到此类 ...

  4. SourceGrid之Grid绑定数据

    private void BindData() { //为绑定的按钮选线增加单击事件 SourceGrid.Cells.Controllers.CustomEvents clickEvent = ne ...

  5. CF1062D Fun with Integers

    思路: 找规律. 实现: #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ...

  6. uvm_subscriber——告诉她我们来过

    Subscribers are basically listeners of an analysis port. They subscribe to a broadcaster and receive ...

  7. AS学习系列[1]——初识Android Studio

    写在前面的话:由于于方老师的高墙所限,网络成了学习Android第一道“拦路虎”.所以,个人以为,在学习Android之前需要了解下FQ技术(仅仅是为了技术学习). 1.AS AS(Android s ...

  8. 忘记Centos7.2下root用户密码后的处理方式

    1)重启系统 重新启动系统后并按f2键,进入如下的界面,再按e键. 2)修改启动内核代码 在代码的linux16行中,将ro rhgb的ro修改为rw init=/sysroot/bin/sh. 3) ...

  9. MySQL出现GROUP BY clause错误解决办法

    #1)当mysql数据库出现如下错误:#which is not functionally dependent on columns in GROUP BY clause; this is incom ...

  10. GP SQL 优化

    1.收集统计信息vacuum full analyze ZCXT.ZCOT_PS_PROJECT; 2.检查表的数据量分布select gp_segment_id,count(*) from fact ...