linux grep 匹配空格_17 个案例,5 分钟简单搞定 Linux 正则表达式!

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

元字符通常在Linux中分为两类:

  1. Shell元字符,由Linux Shell进行解析;
  2. 正则表达式元字符,由vi/grep/sed/awk等文本处理工具进行解析;

正则表达式一般以文本行进行处理,在进行下面实例之前,先为grep命令设置—color参数:

$ alias grep='grep --color=auto' 

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

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

$ 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. 

正则表达式元字符

e4adad3157aea6669d0a276e32b17059.png

特殊的元字符

4532ecd68c3f6889e526aba7d2d4f1f6.png

扩展的正则表达式

b3558b7cc0141289e80884ce2e919210.png

实操

匹配以love开头的所有行

$ grep '^love' re-file  love, how much I adore you. Do you know 

匹配love结尾的所有行

$ grep 'love$' re-file  clover. Did you see them? I can only hope love. 

匹配以l开头,中间包含两个字符,结尾是e的所有行

$ 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的字符

$ 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

$ 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

$ grep '[A-Z]ove' re-file  Lovers were all around us. It is springtime. Oh 

匹配不在A-Z范围内的任何字符行,所有的小写字符

$ 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.

$ grep 'love.' re-file  clover. Did you see them? I can only hope love. 

匹配空格

$ grep '^$' re-file 

匹配任意字符

$ 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次

$ grep 'o{2,4}' re-file  groove. 

重复o字符至少2次

$ grep 'o{2,}' re-file  groove. 

重复0字符最多2次

$ 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. 

重复前一个字符一个或一个以

$ 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 

或,查找多个字符串

$ egrep "gd|good" linux.txt  Linux is a good  gdsystem awxxxx  good 

分组过滤匹配

$ egrep "g(la|oo)d" linux.txt  Linux is a good  glad  good 

感谢支持!后台私信《Linux》获取C++/Linux后台开发进阶视频资料。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/459205.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C# 泛型使用笔记

泛型的基本概念我就不在这重复了,不了解的同学请自行百度。 我主要写下我在项目中要到的泛型实例。献丑了.....有什么不好或不对的地方大家尽可评论留言。 为什么要用泛型? 通过使用泛型,我们可以极大地提高代码的重用度,同时还可…

汇编题目:编写包含多个功能子程序的中断例程

安装一个新的int 7ch中断例程,为显示输出提供如下的功能子程序。 (1)清屏。(2)设置前景色。(3)设置背景色。(4)向上滚动一行 (一)用ah寄…

micropython web ws2812_MicroPython实例之TPYBoard v102炫彩跑马灯WS2812B

一、实验目的了解ws2812b的工作原理学习ws2812b的驱动方法二、实验器材TPYBoard v102 1块ws2812b RGB-Ring-8 1个micro USB数据线 1条杜邦线 若干三、WS2812B的介绍WS2812B是一个集控制电路与发光电路于一体的智能外控LED光源。 其外型与一个5050LED灯珠相同, 每个元…

工作面试经历

这几天去找了找工作,面试了两家IT公司,下面就说说关于这两次面试的经历以及感受。 第一家公司很小,在这就不说它的名字的,公司是一个三室两厅的感觉,客厅里摆了两排电脑,里面有一个员工,老板见我…

mysql数据库表复用_MySQL 数据库之表操作

一、创建表的完整语法create table 表(字段名1 类型 [(宽度) 约束条件],字段名2 类型 [(宽度) 约束条件],字段名3 类型 [(宽度) 约束条件]);1.类型:使用限制字段必须以什么样的数据类型传值约束条件:约束条件是在类型之…

ubuntu命令收集

软件操作: - sudo apt-get install xxx  安装软件 - sudo apt-get --purge remove XXX  卸载软件 - sudo apt-get -f install  修复安装 - sudo dpkg -i [fileName.deb] 安装deb结尾的文件(不会解决依赖) echo XXX  输出到终端 nohup XXX  不挂断地运行命…

你不必害怕,岁月有的是时间让你遇见更好的人(沈善书)

这是我单身生活的第四年。一个人的生活里,总会羡慕别人的爱情。时间久了,我就慢慢告诉自己,其实也不必羡慕别人的爱情,我也可以轰轰烈烈,只是上辈子欠了岁月一个人情,岁月要让我多等待,磨练我的…

postgis 导出 栅格_postgis常见的空间数据的导入导出

本片文章简单记录了postgis涉及的常用数据导入导出的使用方法。1.将osm数据导入postgisosm2pgsql -c -d osm -l -E 4326 -S /usr/share/osm2pgsql/default.style xxxxx.osm这里要注意几个参数,是选用-c还是-a要看你的应用方式,-S这个是在ubuntu下路径不同时使用&…

修复./mysql/proc

mysql数据库只能建不能删的错误提示及处理方法:mysql> drop database testmysqldatabase;ERROR 145 (HY000): Table ./mysql/proc is marked as crashed and should be repaired处理方法:直接在mysql数据库里面使用root帐号登录后,进行修复…

Android有道词典查询功能

有道词典 任务要求&#xff1a;完成查词等功能 因为需要申请API key&#xff0c;这里直接给出地址供使用&#xff1a;http://fanyi.youdao.com/openapi?pathdata-mode 1、activity_main.xml基本格局&#xff08;不做任何说明&#xff09; 代码如下&#xff1a; 1 <Relative…

C++的迭代器Interator

*************************************************** 更多精彩&#xff0c;欢迎进入&#xff1a;http://shop115376623.taobao.com STL视频教程&#xff1a; http://item.taobao.com/item.htm?spma1z10.5-c.w4002-9510581626.21.y9vLuz&id43055362725 ***************…

从零开始学Axure原型设计(高级篇)

如果你熟悉了Axure的部件库&#xff0c;那么你可以得心应手地画出心目中产品的线框图&#xff1b;如果你会用Axure的母版、动态面板功能&#xff0c;那么你应该能够画出一些简单网站的原型图&#xff1b;但只有你精通了Axure的条件逻辑、变量、函数等高级交互&#xff0c;才能将…

python vector 初始化_从零开始搭建机器学习算法框架(python)--计算框架

介绍今天开始一个新的系列&#xff0c;这个系列的目标是用python在不使用任何第三方库的情况下去实现各类机器学习或者深度学习的算法。之所以会有这种想法是因为每当我想提高编程技巧的时候&#xff0c;我总希望能够做一些简单又有趣的小项目练手。我一直对机器学习算法颇感兴…

windows phone 页面主题设计

达到如图这个效果&#xff1a; 1.保证状态栏背景色与主题栏颜色相同需设置状态栏的透明度&#xff0c;代码如下&#xff1a;shell:SystemTray.IsVisible"True" shell:SystemTray.Opacity"0.01" 2.顶部状态栏高度为25&#xff0c;字的上下要留部分空白3.内容…

C++ Vector 使用心得

*************************************************** 更多精彩&#xff0c;欢迎进入&#xff1a;http://shop115376623.taobao.com STL视频教程&#xff1a; http://item.taobao.com/item.htm?spma1z10.5-c.w4002-9510581626.21.y9vLuz&id43055362725 ***************…

JAVA如何才能导出这样的EXCEL?

2019独角兽企业重金招聘Python工程师标准>>> 最近的项目有个需求&#xff0c;需要做个报表&#xff0c;excel如上所示。没有很好的办法&#xff0c;求指教。 转载于:https://my.oschina.net/secret620/blog/611450

python数据分析、挖掘与可视化 慕课答案_Python数据分析、挖掘与可视化(慕课版)...

第1章 Python开发环境的搭建与编码规范 1n1&#xff0e;1 Python开发环境的搭建与使用 1n1&#xff0e;1&#xff0e;1 IDLE 2n1&#xff0e;1&#xff0e;2 Anaconda3 3n1&#xff0e;1&#xff0e;3 安装扩展库 4n1&#xff0e;2 Python编码规范 5n1&#xff0e;3…

Xcode 修改工程名以及注意事项

1、先把整个工程文件夹名改为新的工程名。 2、打开工程&#xff0c;单击&#xff0c;输入新的工程名,会出现&#xff0c;点击确定。 3、回到工程界面&#xff0c;在中选择 Manage Schemes,然后再弹出的对话框&#xff0c;把工程名改为新的名字。 4、最好在工程中&#xff0c;把…

C++语言中multiset的相关用法及扩展

*************************************************** 更多精彩&#xff0c;欢迎进入&#xff1a;http://shop115376623.taobao.com *************************************************** cpp语言中&#xff0c;multiset是<set>库中一个非常有用的类型&#xff0c;它可…

[20160201]db_link与子光标问题.txt

[20160201]db_link与子光标问题.txt --生产系统遇到一个关于db_link产生大量子光标问题&#xff0c;当cursor_sharingforce的情况下&#xff0c;通过测试说明。 --注&#xff1a;这个问题我的测试仅仅存在10.2.0.4,11.2.0.4没有这个问题。 1.环境&#xff1a; SCOTTtest> &a…