正则表达式是一种字符模式,用于在查找过程中匹配制定的字符。

元字符通常在Linux中分为两类:Shell元字符,由Linux Shell进行解析;
正则表达式元字符,由vi/grep/sed/awk等文本处理工具进行解析;
正则表达式一般以文本行进行处理,在进行下面实例之前,先为grep命令设置--color参数:

 class="">$ alias grep='grep --color=auto'

这样每次过滤出来的字符串都会带色彩了。

在开始之前还需要做一件事情,就是创建一个测试用的re-file文件,内容如下:

 class="">$ cat re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.
 class="">$ cat linux.txt
Linux is a good 
god assdxw bcvnbvbjk
greatttttt  wexcvxc
operaaaating  dhfghfvx
gooodfs awrerdxxhkl
gdsystem awxxxx
glad
good
正则表达式元字符

特殊的元字符

扩展的正则表达式

实操
匹配以love开头的所有行
 class="">$ grep '^love' re-file
love, how much I adore you. Do you know
匹配love结尾的所有行
 class="">$ grep 'love$' re-file
clover. Did you see them?  I can only hope love.
匹配以l开头,中间包含两个字符,结尾是e的所有行
 class="">$ grep 'l..e' re-file
I had a lovely time on our little picnic.
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It's hard to get back in the
匹配0个或多个空行,后面是love的字符
 class="">$ grep ' *love' re-file
I had a lovely time on our little picnic.
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
匹配love或Love
 class="">$ grep '[Ll]ove' re-file  # 对l不区分大小写
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
匹配A-Z的字母,其次是ove
 class="">$ grep '[A-Z]ove' re-file
Lovers were all around us. It is springtime. Oh
匹配不在A-Z范围内的任何字符行,所有的小写字符
 class="">$ grep '[^A-Z]' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.
匹配love
 class="">$ grep 'love\.' re-file
clover. Did you see them?  I can only hope love.
匹配空格
 class="">$ grep '^$' re-file
匹配任意字符
class="">$ grep '.*' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.
前面o字符重复2到4次
 class="">$ grep 'o\{2,4\}' re-file
groove.
重复o字符至少2次
 class="">$ grep 'o\{2,\}' re-file
groove.
重复0字符最多2次
 class="">$ grep 'o\{,2\}' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.
重复前一个字符一个或一个以上
 class="">$ egrep "go+d" linux.txt
Linux is a good
god assdxw bcvnbvbjk
gooodfs awrerdxxhkl
good
#####0个或者一个字符
ansheng@Ubuntu:/tmp$ egrep "go?d" linux.txt
god assdxw bcvnbvbjk
gdsystem awxxxx

或,查找多个字符串

 class="">$ egrep "gd|good" linux.txt
Linux is a good
gdsystem awxxxx
good
分组过滤匹配
 class="">$ egrep "g(la|oo)d" linux.txt
Linux is a good
glad
good

3分钟搞定Linux系统正则表达式的更多相关文章

  1. 五分钟搞定 Linux 文档全部知识,就看这篇文章

    作者:无痴迷,不成功 来源:见文末 写在前面 我们都知道Linux是一个支持多用户.多任务的系统,这也是它最优秀的特性,即可能同时有很多人都在系统上进行工作,所以千万不要强制关机,同时,为了保护每个人 ...

  2. 五分钟搞定Linux容器

    [TechTarget中国原创] Linux容器针对特定工作负载提供了全新的灵活性与可能性.存在很多解决方案,但是没有一个解决方案能够像systemd容器那样进行快速部署.给我五分钟,本文将介绍如何使 ...

  3. JS组件系列——又一款MVVM组件:Vue(一:30分钟搞定前端增删改查)

    前言:关于Vue框架,好几个月之前就听说过,了解一项新技术之后,总是处于观望状态,一直在犹豫要不要系统学习下.正好最近有点空,就去官网了解了下,看上去还不错的一个组件,就抽空研究了下.最近园子里vue ...

  4. zookeeper-架构设计与角色分工-《每日五分钟搞定大数据》

    本篇文章阅读时间5分钟左右 点击看<每日五分钟搞定大数据>完整思维导图   zookeeper作为一个分布式协调系统,很多组件都会依赖它,那么此时它的可用性就非常重要了,那么保证可用性的同 ...

  5. windows+mysql集群搭建-三分钟搞定集群

    注:本文来源:  陈晓婵   <  windows+mysql集群搭建-三分钟搞定集群   > 一:mysql集群搭建教程-基础篇 计算机一级考试系统要用集群,目标是把集群搭建起来,保证一 ...

  6. 转载-30分钟搞定后台登录界面(103个后台PSD源文件、素材网站)

    原文:30分钟搞定后台登录界面(103个后台PSD源文件.素材网站)   目录 一.界面预览 二.PSD源文件预览 三.工具分享 四.资源说明 五.素材下载网站 六.下载 去年八月时要做一个OA系统为 ...

  7. 【转】让你10分钟搞定Mac--最简单快速的虚拟安装

    文章出处:让你10分钟搞定Mac--最简单快速的虚拟安装http://bbs.itheima.com/thread-106643-1-1.html (出处: 黑马程序员训练营论坛) 首先说明一下. 第 ...

  8. 一分钟搞定AlloyTouch图片轮播

      一分钟搞定AlloyTouch图片轮播 轮播图也涉及到触摸和触摸反馈,同时,AlloyTouch可以把惯性运动打开或者关闭,并且设置min和max为运动区域,超出会自动回弹.除了一般的竖向滚动,A ...

  9. 五分钟搞定Go.js

    五分钟搞定Go.js  1.基于html5~因为Go.js是一个依赖于HTML5特性的JavaScript库,所以需要确保您的页面声明它是一个HTML5文档,当然需要加载库 <!DOCTYPE ...

随机推荐

  1. HDU 4786 Fibonacci Tree (2013成都1006题)

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. Using Dtrace OEL 6.X

    http://www.hhutzler.de/blog/using-dtrace/ http://docs.oracle.com/cd/E37670_01/E38608/html/dt_sdtparg ...

  3. Revit API判断直线相交关系移动风管

    start )             );         )) )) );         XYZ xyz12 = lCurve1.Curve.get_EndPoint();         XY ...

  4. 大不列颠百科全书Encyclopaedia Britannica Ultimate 2014光盘镜像

    大不列颠百科全书又名大英百科全书,是目前最古老的百科全书之一.大英百科全书每10余年出一个版本,如今已经推出到Encyclopaedia Britannica Ultimate 2014.此次推荐的是 ...

  5. Delphi XE5 Android 运行黑屏卡死的解决方法

    1. 确保正确安装Android SDK: 开始菜单 > 所有程序 > Embarcadero RAD Studio XE5 > > Android Tools > 打开 ...

  6. NSURLRequest with UserAgent

    关于iOS上的http请求还在不断学习,从早先的时候发现原来iOS的http请求可以自动保存cookie到后来的,发现ASIHttpRequest会有User-Agent,到现在发现竟然NSURLRe ...

  7. WebLogic清理缓存

    如果发布到weblogic的工程,登录发现还是原来的代码错误,可尝试清理weblogic缓存: 1.在weblogic控制台中停止应用,删除部署的工程 2.登录weblogic服务器,删除以下目录中的 ...

  8. 设计模式之命令模式(Command)

    from::http://www.cnblogs.com/itTeacher/archive/2012/12/04/2801322.html 命令模式将一个请求封装为一个对象,从而使你可用不同的请求对 ...

  9. OAuth2.0 原理流程及其单点登录和权限控制

    2018年07月26日 07:21:58 kefeng-wang 阅读数:5468更多 所属专栏: Java微服务构架   版权声明:[自由转载-非商用-非衍生-保持署名]-转载请标明作者和出处. h ...

  10. Monitoring an IBM JVM with VisualVM

    Monitoring an IBM JVM with VisualVM 分类: Java 2013-06-09 16:15 250人阅读 评论(0) 收藏 举报 JDK6 update 7 and o ...