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

元字符通常在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. H5在Android 4.4中WebView兼容性问题

    项目中使用到了Vue.YDUI.webpack,部分页面在Android WebView中出现了样式问题,卡顿等等: 1.promise不识别——需要使用babel-polyfill. 2.由于系统限 ...

  2. systemtap 脚本示例

    .[root@localhost ~]# stap -v -e 'probe vfs.read {printf("read performed\n"); exit()}' Pass ...

  3. 如何在socket编程的Tcp连接中实现心跳协议

    from http://blog.csdn.net/nyist327/article/details/39586203 心跳包的发送,通常有两种技术方法1:应用层自己实现的心跳包 由应用程序自己发送心 ...

  4. Revit API风管对齐

    start [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] ].Origin, list ...

  5. Delphi的命令行编译命令

    Borland出品的Delphi,有着闪电般的编译速度,但是在界面控件使用较多.工程项目较大的时候,编译一个工程仍需要一段时间,打开庞大的Delphi IDE,也需要时间.其实,在一个工程开发结束,调 ...

  6. arcgis导oracle多步操作产生错误。请检查每一步的状态值。" 如何解决?

    你知你用的什么数据引擎,ADO? 我以前碰过类似的,我有两个方案:   1.升ado到2.7以上      2.不要用microsoft oledb provider for oracle,而要用or ...

  7. 安全:CSRF

    原文地址:http://baike.baidu.com/view/1609487.htm?fr=aladdin. 攻击通过在授权用户访问的页面中包含链接或者脚本的方式工作.例如:一个网站用户Bob可能 ...

  8. Android之2次打开添加友盟统计代码,后缀会添加广告

    这里首先列明步骤, 做一个标识仅此而已. 1. 首先使用apktool来反编译你待需要加入友盟统计的apk包, 具体如何使用与配置apktool, 请参考我关于apktool配置的文章. 2. 然后自 ...

  9. sys.usb.config webcam

    setprop persist.sys.usb.config webcamecho 0 > /sys/devices/virtual/android_usb/android0/enableech ...

  10. .Net Excel操作之NPOI(一)简介

    一.NPOI简介 NPOI是一个开源项目,可以读/写xls,doc,ppt文件,有着广泛的应用. 使用NPOI能够帮助开发者在没有安装微软Office的情况下读写Office 97-2003的文件,支 ...