Shell 编程 正则表达式

本篇主要写一些shell脚本正则表达式的使用基础。
概述
正则表达式分为基础正则表达式(Regular Expression)与扩展正则表达式(Extended Regular Expression)。
它不是一个工具程序,而是一个字符串处理的标准依据,是使用单个字符串搜索、匹配一系列符合某个语法规则的字符串。
它由普通字符(a~z)以及特殊字符(元字符)组成。
linux 文本处理工具
| 文本处理工具 | 基础正则表达式 | 扩展正则表达式 |
|---|---|---|
| vi编辑器 | 支持 | / |
| grep | 支持 | / |
| egrep | 支持 | 支持 |
| sed | 支持 | / |
| awk | 支持 | 支持 |
基础正则表达式
- 基础正则表达式常见元字符
^:匹配输入字符串的开始位置。在方括号表达式中使用,表示不包含该字符集合。要匹配^字符本身,使用\^
$:匹配输入字符串的结尾位置。如果设置了RegExp对象的Multiline属性,则$也匹配\n或\r。要匹配$字符本身,请使用\$
.:匹配除\r\n之外的任何单个字符
\:将下一个字符标记为特殊字符、原义字符、向后引用、八进制转义符。例如,n匹配字符n。 \n匹配换行符。序列\\匹配\,而\(则匹配(
*:匹配前面的子表达式零次或多次。要匹配*字符,请使用\*
[]:字符集合。匹配所包含的任意一个字符。例如,[abc]可以匹配plain中的a
[^]:赋值字符集合。匹配未包含的一个任意字符。例如,[^abc]可以匹配plain中plin中的任何一个字母
[n1-n2]:字符范围。匹配指定范围内的任意一个字符。例如,[a-z]可以匹配a到z范围内的任意一个小写字母字符。
{n}:n是一个非负整数,匹配确定的n次。例如,o{2}不能匹配Bob中的o,但是能匹配food中的两个o
{n,}:n是一个非负整数,至少匹配n次。例如,o{2,}不能匹配Bob中的o,但能匹配foooood中的所有o。o{1,}等价于o+。o{0,}则等价于o*
{n,m}:m和n均为非负整数,其中n<=m,最少匹配n次且最多匹配m次
- 使用
grep做示例,首先准备一个测试文件
he was short and fat.
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
The year ahead will test our political establishment to the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
特定字符
-n:显示行号
-i:不区分大小写
-v:反向选择
[root@localhost ~]# grep -n 'the' test.txt
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to the limit.
[root@localhost ~]# grep -in 'the' test.txt
3:The home of Football on BBC Sport online.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to the limit.
[root@localhost ~]# grep -vn 'the' test.txt
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
3:The home of Football on BBC Sport online.
7:PI=3.141592653589793238462643383249901429
8:a wood cross!
9:Actions speak louder than words
10:
11:
12:#woood #
13:#woooooood #
14:AxyzxyzxyzxyzC
15:I bet this place is really spooky late at night!
16:Misfortunes never come alone/single.
17:I shouldn't have lett so tast.
集合“[]”
- 中括号“[]”中无论又几个字符,都只匹配其中一个
[root@localhost ~]# grep -n 'sh[io]rt' test.txt
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
- 匹配重复单个字符
oo
[root@localhost ~]# grep -n 'oo' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
12:#woood #
13:#woooooood #
15:I bet this place is really spooky late at night!
- 查找
oo前不是w的字符串
[root@localhost ~]# grep -n '[^w]oo' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
12:#woood #
13:#woooooood #
15:I bet this place is really spooky late at night!
- 查找
oo前没有小写字母的字符串
[root@localhost ~]# grep -n '[^a-z]oo' test.txt
3:The home of Football on BBC Sport online.
- 查找包含数字的行
[root@localhost ~]# grep -n '[0-9]' test.txt
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429
行首“^”
- 查找已
the为行首的行
[root@localhost ~]# grep -n '^the' test.txt
4:the tongue is boneless but it breaks bones.12!
- 查找是小写字母开头的行
[root@localhost ~]# grep -n '^[a-z]' test.txt
1:he was short and fat.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
8:a wood cross!
- 查找是大写字母开头的行
[root@localhost ~]# grep -n '^[A-Z]' test.txt
2:He was wearing a blue polo shirt with black pants.
3:The home of Football on BBC Sport online.
6:The year ahead will test our political establishment to the limit.
7:PI=3.141592653589793238462643383249901429
9:Actions speak louder than words
14:AxyzxyzxyzxyzC
15:I bet this place is really spooky late at night!
16:Misfortunes never come alone/single.
17:I shouldn't have lett so tast.
- 查找不是字母开头的行
[root@localhost ~]# grep -n '^[^a-zA-Z]' test.txt
12:#woood #
13:#woooooood #
行尾“$”
- 查找以
.结尾的行,需要使用转义字符
[root@localhost ~]# grep -n '\.$' test.txt
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to the limit.
16:Misfortunes never come alone/single.
17:I shouldn't have lett so tast.
- 查找空白行
[root@localhost ~]# grep -n '^$' test.txt
10:
11:
任意一个字符“.”
- 查找
w和d中间有两个字符的字符串
[root@localhost ~]# grep -n 'w..d' test.txt
5:google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words
重复字符“*”
- 查找至少两个以上的
o的字符串,*代表重复前面的一个字符零个或多个
[root@localhost ~]# grep -n 'ooo*' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
12:#woood #
13:#woooooood #
15:I bet this place is really spooky late at night!
- 查找
w和d中间有两个至少有一个o的字符串
[root@localhost ~]# grep -n 'woo*d' test.txt
8:a wood cross!
12:#woood #
13:#woooooood #
- 查找
w和d中间可有可无的字符串
[root@localhost ~]# grep -n 'w.*d' test.txt
1:he was short and fat.
5:google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words
12:#woood #
13:#woooooood #
- 查找任意数字
[root@localhost ~]# grep -n '[0-9][0-9]*' test.txt
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429
连续字符范围“{}”
- 查找有连续两个
o的字符串,需要转义
[root@localhost ~]# grep -n 'o\{2\}' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
12:#woood #
13:#woooooood #
15:I bet this place is really spooky late at night!
- 查找
w和d中间包含2~5个o的字符串
[root@localhost ~]# grep -n 'wo\{2,5\}d' test.txt
8:a wood cross!
12:#woood #
- 查找
w和d中间包含两个以上o的字符串
[root@localhost ~]# grep -n 'wo\{2,\}d' test.txt
8:a wood cross!
12:#woood #
13:#woooooood #
扩展正则表达式
- 扩展正则表达式常见元字符
+:重复一个或者一个以上的前一个字符
?:零个或者一个的前一个字符
|:使用或者or的方式找出多个字符
():查找组字符串
()+:辨别多个重复的组
- 使用
rgrep做示例,查询w和d之间包含一个以上o的字符串
[root@localhost ~]# egrep -n 'wo+d' test.txt
8:a wood cross!
12:#woood #
13:#woooooood #
- 查询
bet和best这两个字符串
[root@localhost ~]# egrep -n 'bes?t' test.txt
5:google is the best tools for search keyword.
15:I bet this place is really spooky late at night!
- 查询
of或者if或者on字符串
[root@localhost ~]# egrep -n 'of|is|on' test.txt
3:The home of Football on BBC Sport online.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to the limit.
9:Actions speak louder than words
15:I bet this place is really spooky late at night!
16:Misfortunes never come alone/single.
- 查询
tast或者test字符串
[root@localhost ~]# egrep -n 't(a|e)st' test.txt
6:The year ahead will test our political establishment to the limit.
17:I shouldn't have lett so tast.
- 查询开头的
A结尾是C,中间有一个以上的xyz字符串
[root@localhost ~]# egrep -n 'A(xyz)+C' test.txt
14:AxyzxyzxyzxyzC
Shell 编程 正则表达式的更多相关文章
- [shell编程]正则表达式
如果在shell脚本中处理数据文件,那么我们就必须熟悉正则表达式.正则表达式是用来过滤数据流中文本的模式模板,模式由标准文本字符和特殊字符组成.正则表达式用特殊字符来匹配一系列一个或多个字符,要想掌握 ...
- linux笔记:shell编程-正则表达式
正则表达式与通配符(正则表达式匹配字符串,是包含匹配:通配符匹配文件名,是完全匹配.): 基础正则表达式: 正则表达式示例:
- Linux学习 -- Shell编程 -- 正则表达式
正则表达式与通配符 正则 -- 匹配字符串 -- 包含匹配 grep.awk.sed等 通配符 -- 匹配文件名 -- 完全匹配 ls.find.cp等 基础正则表达式
- Shell编程—正则表达式
1什么是正则表达式 1.1定义 正则表达式是你所定义的模式模板,Linux工具可以用它来过滤文本.Linux 工具(比如sed编辑器或gawk程序)能够在处理数据时使用正则表达式对数据进行模式匹配. ...
- Linux学习——shell编程之正则表达式和字符处理命令
shell编程之正则表达式 一 正则表达式 1 什么是正则表达式 正则表达式用于描述字符排列和匹配模式的一种语法规则.它主要用于字符串的模式分隔.匹配.查找及替换操作. 2 shell编程之正则表达式 ...
- 第5天(半天)【shell编程初步、grep及正则表达式】
第5天(半天)[shell编程初步.grep及正则表达式] shell编程初步(01)_recv shell脚本:文本文件 #!:/bin/bash #!:/usr/bin/python #!:/us ...
- 05 shell编程之正则表达式
正则表达式&&文本处理利器 学习目标: l 掌握正则表达式的运用 l 掌握sed.awk文本处理工具的使用 目录结构: 正则表达式 正则表达式概述 l 正则表达式:使用单个字 ...
- Linux学习笔记(17) Shell编程之基础
1. 正则表达式 (1) 正则表达式用来在文件中匹配符合条件的字符串,正则是包含匹配.grep.awk.sed等命令可以支持正则表达式:通配符用来匹配符合条件的文件名,通配符是完全匹配.ls.find ...
- Linux Shell编程入门
从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操 ...
随机推荐
- Computer Network Chapter4 solution
1.以太网使用曼彻斯特编码,效率50% 2.侦听信道时间:来回延时时间(10usec):发送数据(25.6usec): 3.单向时延t=S(距离)/V(电缆传输速率):最小帧长=2*t*C(数据传输速 ...
- sqler 2.2 发布了,支持定时任务以及触发器
sqler 在10前发布了,2.2 添加了定时任务以及触发器(webhook),都是比较方便的功能, 同时我也修改了dockerfile, 做了构建,添加了功能支持,同时push 到了dockerhu ...
- Python例题集
例题1:任意输入一组数据比较其最大值并记录输入的数据个数. 源代码: def max(*a): m=a[0] i=0 for x in a: i+=1 if x>m: m=x print('参数 ...
- Linux系统运维笔记(6),CentOS 7.6双网卡路由配置
Linux系统运维笔记(6),CentOS 7.6双网卡路由配置. 一,先确认系统版本: [root@localhost ~]# cat /etc/redhat-releaseCentOS Linux ...
- CodeForces 487E Tourists(圆方树+线段树+树链剖分)
题意 \(n\) 个点 \(m\) 条边的无向连通图,每个点有点权,\(q\) 个要求,每次更新一个点的点权或查询两点间路径权值最小的点最小的路径. 思路 算是圆方树的板子吧?圆方树处理的主要 ...
- EFCore代码实践
参考:https://www.cnblogs.com/Wddpct/p/6835574.html 控制台程序依赖注入参考:https://www.cnblogs.com/Wddpct/p/721920 ...
- Docker下构建centos7容器无法使用systemctl命令的解决办法
最近在使用docker 构建centos7 容器时,发现无法使用systemctl 命令.后来万能的百度解决了问题,随记之以备后用. 解决办法: docker run --privileged -it ...
- vulnhub之SP:Harrison靶机
下载地址:‘https://www.vulnhub.com/entry/sp-harrison,302/’ 环境:靶机放在virtualbox上运行,网卡模式 攻击机:kali Linux运行在VMw ...
- 使用velero进行kubernetes灾备
使用velero可以对集群进行备份和恢复,降低集群DR造成的影响.velero的基本原理就是将集群的数据备份到对象存储中,在恢复的时候将数据从对象存储中拉取下来.可以从官方文档查看可接收的对象存储,本 ...
- 微信小程序子组件样式不起作用的解决办法
今天我在编写微信小程序项目时,发现父组件引用子组件过后,子组件的样式不起作用,在上网查了很多解决办法后,成功解决了这一问题. 解决办法: 1.在全局样式文件app.wxss中引入子组件的样式,如 @i ...