小阿轩yx-Shell编程之正则表达式与文本处理器
正则表达式
(RegularExpression,RE)
正则表达式概述
正则表达式的定义
又称
- 正规表达式
- 常规表达式
代码中常简写为 regex、regexp 或 RE
正则表达式
-
使用单个字符串来描述、匹配一系列符合某个句法规则的字符串
-
简单来说, 是一种匹配字符串的方法,通过一些特殊符号,实现快速查找、删除、替换某个特定字符串
-
由普通字符与元字符组成的文字模式。模式用于描述搜索文本时要匹配的一个或多个字符串。
-
作为一个模板,将某个字符模式与所搜索的字符串进行匹配。
-
其中普通字符包括大小写字母、数字、标点符号及一些其他符号
-
元字符则是指那些在正则表达式中具有特殊意义的专用字符,可以用来规定其前导字符(即位于元字符 前面的字符)在目标对象中的出现模式
-
一般用于脚本编程与文本编辑器中,很多文本处理器与程序设计语言均支持
正则表达式用途
- 计算机用户用的不多
- 对于系统管理员则是必备技能之一,是非常重要的
系统运行过程中会产生非常重要的大量信息,有些则仅是告知的信息
- 快速提取"有问题"的信息,可以将运维工作变得更加简单、 方便
很多软件支持正则表达式,常见于邮件服务器
很多服务器软件也支持正则表达式
基础正则表达式
- 基本正则表达式:常用正则表达式最基础的部分,处理工具grep与sed支持基础正则表达式
- 扩展正则表达式:egrep与awk支持扩展正则表达式
grep
- -a 不要忽略二进制数据
- -A<显示列数> 除了显示符合范本样式的那一行之外,并显示该行之后的内容
- -b 在显示符合范本样式的那一行之外,并显示该行之前的内容
- -c 计算符合范本样式的列数
- -C<显示列数>或-<显示列数> 除了显示符合范本样式的那一列之外,并显示该列之前后的内容
- -d<进行动作> 当指定要查找的是目录而非文件时,必须使用这项参数,否则grep命令将回报信息并停止动作。-e<范本样式> 指定字符串作为查找文件内容的范本样式\
- -E 将范本样式为延伸的普通表示法来使用,意味着使用能使用扩展正则表达式
- -f<范本文件> 指定范本文件,其内容有一个或多个范本样式,让grep查找符合范本条件的文件内容,格式为每一列的范本样式
- -F 将范本样式视为固定字符串的列表
- -G 将范本样式视为普通的表示法来使用
- -h 在显示符合范本样式的那一列之前,不标示该列所属的文件名称
- -H 在显示符合范本样式的那一列之前,标示该列的文件名称
- -i 忽略字符大小写的差别
- -l 列出文件内容符合指定的范本样式的文件名称
- -L 列出文件内容不符合指定的范本样式的文件名称
- -n 在显示符合范本样式的那一列之前,标示出该列的编号
- -q 不显示任何信息
- -R/-r 此参数的效果和指定"-d recurse"参数相同
- -s 不显示错误信息
- -v 反转查找
- -w 只显示全字符合的列
- -x 只显示全列符合的列
- -y 此参数效果跟"-i"相同
- -o 只输出文件中匹配到的部分
创建测试文件
[root@localhost ~]# cat test.txt
he was short and fat.
he was weating 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.
PI=3.14
a wood cross!
Actions speak louder than words
#woood #
#woooooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
查找特定字符
查看包含 the 的行
[root@localhost ~]# grep -ni '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.
注
-n:显示行号
-i:不区分大小写
-v:不包含指定字符
利用 [] 查找集合字符
[root@localhost ~]# grep -n 'sh[io]rt' test.txt
1:he was short and fat.
2:he was weating a blue polo shirt with black pants.
注
\]:中括号内不管写几个字符,都只匹配一个,表示匹配其中的任何一个字符
**查找字母 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.
9:#woood #
10:#woooooooood #
12: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.
(注:字母前直接用\^,表示取反 \[ \] 前用\^,表示以括号中的字符开头)
#### **查找行首与行位**
**查看以 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.
2:he was weating a blue polo shirt with black pants.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
7:a wood cross!
**查询以大写字母开头的行**
[root@localhost ~]# grep -n '^[A-Z]' test.txt
3:The home of Football on BBC Sport online.
6:PI=3.14
8:Actions speak louder than words
11:AxyzxyzxyzxyzC
12:I bet this place is really spooky late at night!
13:Misfortunes never come alone/single.
14:I shouldn't have lett so tast.
**查看以非字母开头的行**
[root@localhost ~]# grep -n '^[^a-zA-Z]' test.txt
9:#woood #
10:#woooooooood #
**查看以点结尾的行**
[root@localhost ~]# grep -n '\.$' test.txt
1:he was short and fat.
2:he was weating 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.
13:Misfortunes never come alone/single.
14:I shouldn't have lett so tast.
**查询空行**
[root@localhost ~]# grep -n '^$' test.txt
**查询非空行**
[root@localhost ~]# grep -n -v '^$' test.txt
#### **查找任意字符和重复字符**
查找包含四字符的单词的行,单词以w开头,以d结尾
[root@localhost ~]# grep -n 'w..d' test.txt
5:google is the best tools for search keyword.
7:a wood cross!
8:Actions speak louder than words
(注:一个点代表一个字符)
**查询至少包含两个字母o(oo)字符串的行**
[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.
7:a wood cross!
10:#woood #
11:#woooooooood #
13:I bet this place is really spooky late at night!
(注:ooo\*:前两个o是条件,表示包含两个o;然后是o\*,表示后面有零个或多个重复o)
**查找行,行中单词包含w开头和d结尾,中间至少一个字母o**
[root@localhost ~]# grep -n 'woo*d' test.txt
7:a wood cross!
10:#woood #
11:#woooooooood #
**查询以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.
7:a wood cross!
8:Actions speak louder than words
10:#woood #
11:#woooooooood #
**查询包含数字的行**
[root@localhost ~]# grep -n '[0-9][0-9]*' test.txt
4:the tongue is boneless but it breaks bones.12!
6:PI=3.14
#### **查找连续字符范围**
**查询包含两个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.
7:a wood cross!
10:#woood #
11:#woooooooood #
13:I bet this place is really spooky late at night!
(注:'o\\{2\\}':表示两个字母o)
**w开头,d结尾中间有2--5个o**
[root@localhost ~]# grep -n 'wo\{2,5\}d' test.txt
7:a wood cross!
10:#woood #
**w开头,d结尾中间有2个以上o**
[root@localhost ~]# grep -n 'wo\{2,\}d' test.txt
7:a wood cross!
10:#woood #
11:#woooooooood #
#### 扩展正则表达式
[root@localhost ~]# grep -v '^$' /etc/ssh/sshd_config | grep -v '^#'
**用扩展正则表达式表示**
**基础正则表达式包含两个定位元字符**
* "\^"(行首)
* "$"(行尾)
### 扩展正则表达式
**好处**
* 简化整个指令
### 文本处理器
**Shell编程三剑客**
* **grep**
* **sed**
* **awk**
**(注:Shell 编程中经常用到的文本处理工具)**
#### sed工具文本解析转换工具
sed (Stream EDitor)
##### 优点
* 强大
* 简单
* 可以读取文本,并根据指定的条件对文本内容进行编辑(删除、替换、添加、移动等),最后输出所有行或者仅输出处理的某些行
* 可以在无交互的情况下实现相当复杂的文本处理操作
广泛应用于 Shell 脚本中,完成各种自动化处理任务
##### 工作流程包括
* 读取:sed 从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的缓冲区中(又称模式空间,pattern space)。
* 执行:默认情况下,所有的 sed 命令都在模式空间中顺序地执行,除非指定了行的地址,否则 sed 命令将会在所有的行上依次执行。
* 显示:发送修改后的内容到输出流。在发送数据后,模式空间将会被清空。
##### **sed 命令常见用法**
**sed 命令两种格式**
sed [选项] '操作' 参数
sed [选项] -f scriptfile 参数
"参数"指操作的目标文件,当存在多个操作对象时用,文件之间用逗号","分隔
scriptfile表示脚本文件,需要用"-f"选项指定,当脚本文件出现在目标文件之前时,表示通过指定的脚本文件来处理输入的目标文件
**sed 常用选项**
* -n :使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN 的数据一般都会被列出到终端上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来
* -e :直接在命令列模式上进行 sed 的动作编辑
* -f :直接将 sed 的动作写在一个文件内, -f filename 则可以运行 filename 内的 sed 动作
* -r :sed 的动作支持的是延伸型正规表示法的语法。(默认是基础正规表示法语法)
* -i :直接修改读取的文件内容,而不是输出到终端
**常见的 sed 命令选项主要包含**
* -e 或--expression=:表示用指定命令或者脚本来处理输入的文本文件。
* -f 或--file=:表示用指定的脚本文件来处理输入的文本文件。
* -h 或--help:显示帮助。
* -n、--quiet 或 silent:表示仅显示处理后的结果。
* -i:直接编辑文本文件。
**操作需要在 5~ 20 行之间进行,则表示为"5,20 动作行为"。常见的操作包括**
* -a:增加,在当前行下面增加一行指定内容
* -c:替换,将选定行替换为指定内容
* -d:删除,删除选定的行
* -i:插入,在选定行上面插入一行指定内容
* -p:打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以 ASCII 码输出。其通常与"-n"选项一起使用
* -s:替换,替换指定字符
* -y:字符转换
#### **用法**
**输出符合条件的文本(p 表示正常输出)**
输出所有内容
[root@localhost ~]# sed -n 'p' test.txt
输出第三行
[root@localhost ~]# sed -n '3p' test.txt
输出3~5行
[root@localhost ~]# sed -n '3,5p' test.txt
输出所有奇数行
[root@localhost ~]# sed -n 'p;n' test.txt
输出所有偶数行
[root@localhost ~]# sed -n 'n;p' test.txt
输出第1~5行之间的奇数行
[root@localhost ~]# sed -n '1,5{p;n}' test.txt
输出第10行至文件尾之间的偶数行
[root@localhost ~]# sed -n '10,${n;p}' test.txt
注释:此命令中,读取的第一行是文件的第10行,读取的第二行,是文件的第11行,依次类推
输出包含the的行
[root@localhost ~]# sed -n '/the/p' test.txt
输出从第4行开始至第一个包含the的行
[root@localhost ~]# sed -n ' 4,/the/p' test.txt
输出包含the的行所在的行号
[root@localhost ~]# sed -n '/the/=' test.txt
注释:=用来输出行号
输出以PI开头的行
[root@localhost ~]# sed -n '/^PI/p' test.txt
输出包含单词wood的行
[root@localhost ~]# sed -n '/\