1、正则表达式定义
■ 正则表达式,又称正规表达式、常规表达式
■ 使用字符串来描述、匹配一系列符合某个规则的字符串
■ 正则表达式组成
● 普通字符
◆ 大小写字母、数字、标点符号及一些其他符号
● 元字符
◆ 在正则表达式中具有特殊意义的专用字符
■ 正则表达式层次
● 基础正则表达式
● 扩展正则表达式
■ Linux中文本处理工具
● grep
● egrep
● sed
● awk
2、基础正则表达式元字符
■ 基础正则表达式是常用的正则表达式部分
■ 除了普通字符外,常见到以下元字符
● \: 转义字符,\!、 \n等
● ^: 匹配字符串开始的位置
◆ 例: ^a、 ^the、 ^#
● $: 匹配字符串结束的位置
◆ 例: word$
● . :匹配除\n之外的任意的一个字符
◆ 例: go.d、 g..d
● * :匹配前面子表达式0次或者多次
◆ 例: goo*d、 go.*d
● [list] :匹配ist列表中的一个字符
◆ 例: go[ola]d 、[abc]、 [a-z]、 [a-z0-9]
● [^list] :匹配任意不在list列表中的一个字符
◆ 例: [^a-z]、 [^0-9]、 [^A-Z0-9]
● \{n,m\} :匹配前面的子表达式n到m次,有\{n\}、 \{n,\}、\{n,m\}三种格式
◆ 例: go\{2\}d、 go\{2,3\}d、 go\{2,\}d
3、sed 工具
■ sed(StreamEDitor)
● 一个强大而简单的文本解析转换工具,可以读取文本,并根据指定的条件对文本内容进行编辑(删除、替换、添加、移动等),最后输出所有行或者仅输出处理的某些行。
■ sed的工作流程主要包括读取、执行和显示三个过程
● 读取: sed从输入流(文件、管道、标准输入)中读取一-行内容并存储到临时的缓冲区中(又称模式空间,pattern space)
● 执行:默认情况下,所有的sed命令都在模式空间中顺序地执行,除非指定了行的地址,否则sed命令将会在所有的行上依次执行。
● 显示:发送修改后的内容到输出流。在发送数据后,模式空间将会被清空。在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。
注意:默认情况下所有的sed命令都是在模式空间内执行的,因此输入的文件并不会发生任何变化,除非是用重定向存储输出。
■ sed命令常见用法
● sed [选项] ‘操作’ 参数
● sed [选项] -f scriptfile 参数
■ 常见的sed命令选项主要包含以下几种:
- -e script: 指定sed编辑命令
- -f scriptfile: 指定的文件中是sed编辑命令
- -h或–help: 显示帮助
- -n、–quiet或silent:表示仅显示处理后的结果。
- -i: 直接编辑文本文件
■ 常见的操作包括以下几种:
- a: 增加,在当前行下面增加一行指定内容。
- c: 替换,将选定行替换为指定内容。
- d: 删除,删除选定的行。
- i: 插入,在选定行上面插入一行指定内容。
- p: 打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以ASCII码输出。其通常与“-n”选项一起使用。
- s: 替换,替换指定字符。
- y: 字符转换。
■ 迁移符合条件的文本
- H: 复制到剪贴板;
- g、G: 将剪贴板中的数据覆盖/追加至指定行
- w: 保存为文件
- r: 读取指定文件
- a:追加指定内容。
4、grep工具
■ grep的使用规则
● -n:表示显示行号
● -i:表示不区分大小写
● -v:表示反向过滤
● [ ]:查找集合字符
■ 创建测试用的txt文件
vi test.txt
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
l bet this place is really spooky late at night!
Misfortunes never come alone/single.
l shouldn’t have lett so tast.
■ [root@server3 ~]# grep -n ‘the’ test.txt
匹配文章中有the的行并显示行号
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.
若反向选择,如查找不包含“the”字符的行,则需要通过grep命令的“-v选项实现,并配合“-n”一起使用显示行号。
■ [root@server3 ~]# grep -vn ‘the’ test.txt
反向过滤包含the的行,并显示行号(匹配不包含the的行,并显示行号)
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 : #woood#
11 : #w0000000d#
12:
13: AxyzxyzxyzxyzC
14:l bet this place is really spooky late at night!
15:Misfortunes never come alone/ single .
16:l shouldn’t have lett so tast.
利用中括号“[]”来查找集合字符说“[io]”表示匹配“i”或者“o”
■ [root@server3 ~]# grep -n ‘sh[oi]rt’ test.txt
1: he was short and fat.
2: He was wearing a blue polo shirt with black pants.
查找包含重复单个字符“oo”时,只需要执行以下命令即可。
■ [root@server3 ~]# grep -n ‘oo’ test.txt或者
[root@server3 ~]# 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 !
10: #woood#
11: #w0000000d#
14: l bet this place is really spooky late at night!
若查找“oo”前面不是“w”的字符串,只需要通过集合字符的反向选择“[^]”来实现该目的。例如执行“grep -n’[^w]oo’ test.txt”命令表示在 test.txt 文本中查找“oo”前面不是“w”的字符串
■ [root@server3 ~]# grep -n ‘[^w]oo’ test.txt
3: The home of Football on BBC Sport online.
5: google is the best tools for search keyword.
10: #wo0od#
11: #wo00000od#
14: l bet this place is really spooky late at night!
若不希望“oo”前面存在小写字母,可以使用“grep -n ‘[ ^a-z]oo’ test.txt”命令实现
■ [root@server3 ~]# grep -n ‘[^a-z]oo’ test.txt
3: The home of Football on BBC Sport online.
查找包含数字的行可以通过“grep -n ‘[0-9]’ test.txt”命令来实现
■ [root@server3 ~]# grep -n ‘[0-9]’ test.txt
4: the tongue is boneless but it breaks bones.12!
7: PI=3.141592653589793238462643383249901429.
查找行首"^“与行尾字符”$"
■ [root@server3 ~]# grep -n ‘^the’ test.txt
4: the tongue is boneless but it breaks bones. 12!
查询大写字母开头的行则使用^ [A-Z]规则
■ [root@server3 ~]# 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 .
13: AxyzxyzxyzxyzC15: Misfortunes never come alone/single.
查询不以字母开头的行则使用“^ [^a-zA-Z]规则
■ [root@server3 ~]# grep -n ’ ^ [^a-zA-Z]’ test.txt
10: #w000d#
11: #woooooood#
小数点. 在正则表达式中也是一个元字符,所以在这里需要用转义字符“\”将具有特殊意义的字符转化成普通字符
■ [root@server3 ~]# 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 .
7: PI=3.141592653589793238462643383249901429.
9: Actions speak louder than words .
15: Misfortunes never come alone/single.
16: l shouldn’t have lett so tast.
查询空白行时,执行“grep -n‘^$’test.txt”命令
■ [root@server3 ~] # grep -n ‘^$’ test.txt
12:
正则表达式中小数点(.)也是一个元字符,代表任意一个字符
■ [root@server3 ~]# 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.
若想要查询oo、ooo、ooooo 等资料,则需要使用星号()元字符。但需要注意的是,“”代表的是重复零个或多个前面的单字符。“o*”表示拥有零个(即为空字符)或大于等于一个“o”的字符,因为允许空字符,所以执行“grep -n 'o*’ test.txt”命令会将文本中所有的内容都输出打印。如果是“oo*”,则第一个o 必须存在,第二 个o则是零个或多个o,所以凡是包含o、oo、ooo、ooo,等的资料都符合标准。同理,若查询包含至少两个o以上的字符串,则执行“grep -n ‘ooo*’ test.txt”命令即可
■ [root@server3 ~]# grep -n ‘ooo*’ test.txt //匹配包含2个oo以上的
3: The home of Football on BBC Sport online.
5: google is the best tools for search keyword.
8: a wood cross!
10: #w00od#
11: #w000o0000d#
14: I bet this place is really spooky late at night!
查询以w开头d结尾,中间包含至少一个o 的字符串
■ [root@server3 ~]# grep -n ‘woo*d’ test.txt
8: a wood cross !
10: #woood#
11: #wooo0oood#
执行以下命令即可查询以w开头d 结尾,中间的字符可有可无的字符串.
■ [root@server3 ~]# 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.
10: #woood#
11: #woo0000od
■ [root@server3 ~]# grep -n ‘[0-9][0-9]*’ test.txt
//可以配备包含0-9的,也可以是包含0-9以为的两位数等数字
4: the tongue is boneless but it breaks bones.12 !
7: PI=3.141592653589793238462643383249901429.
查找连续字符范围“{}”因为“{}”在 Shell 中具有特殊意义,所以在使用“{}”字符时,需要利用转义字符“\”,将“{}”字符转换成普通字符。查询两个o的字符。
■ [root@server3 ~]# 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!
10: #w00od#
11: #woooooood#
14: l bet this place is really spooky late at night!
查询以w开头以d结尾,中间包含2~5个o的字符串.
■ [root@server3 ~]# grep -n ‘wo{2,5}d’ test.txt
8: a wood cross !
10: #woood#
查询以w开头以d 结尾,中间包含2个或2个以上o的字符串.
■ [root@server3 ~]# grep -n ‘wo{2,}d’ test.txt
8: a wood cross !
10: #woood#
11: #w0000000d#