第十一课(2018-06-04)
目录
- 正则表达式
- 扩展正则表达式
- grep
- sed
- awk
1. 正则表达式
Regular Expression, 通过一些特殊字符的排列, 配合上部分工具, 达到"搜索/取代/删除"具有某些特征的字符串的目的.
说白了, RE 就是用于标记字符串的一种表达式
符号 | 含义 | 示例 |
---|---|---|
[list] |
代表一个字符, 方括号内为该字符的穷举 |
[0-9] 代表数字; [a-z] 代表全部小写;[akw] 代表 a/k/w 中选一个 |
[^] |
代表一个字符, 表示取反, 即不取方括号内的任何值 |
[^0-9] 代表非数字;[^a-z] 代表非小写; [akw] 代表不是 a/k/w 中的任何一个 |
^word |
代表行首, 后接特征, 代表以该特征作为行首 |
^9 代表以9作为行首 |
word$ |
代表行尾, 前接特征, 代表以该特征作为行尾 |
9$ 代表以9作为行尾 |
. |
代表有且只有任意一个字符 |
a.g 代表 a 和 g 之间有且只有任意一个字符 如 abg akg a/g a%g 等 |
* |
代表0~无穷个前面那一个, 前接一个字符 |
.* 代表任意个任意字符; a* 代表0~无穷个 a; aa* 代表1~无穷个 a |
{n,m} |
代表某个字符连续重复的次数, 前接字符, 中间为次数范围, 但花括号在 RE 中属于特殊字符, 需要转义 |
o\{2\} 代表2个 o; o\{2,5\} 代表2至5个连续的 o; |
2. 扩展正则表达式
RE Extended 字符 | 含义 | 示例 |
---|---|---|
char+ |
>=1 个char |
a+ 代表1个或更多的a |
char? |
<=1 个char |
a? 代表0个或1个的a |
PATTERN1|PATTERN2 |
用或的方式进行匹配 |
gd | good :代表查找关键字 gd 或 good |
( ) |
查找字符,常与 | 配合使用 |
g(la|oo)d 代表查找关键字 glad 或 good |
( )+ |
同一字符多次重复的查找 |
(xyz)+ 代表查找关键字 xyz 或 xyzxyz 或 xyzxyzxyz... |
3. grep
查找、筛选。根据特征进行文本的查找。以行为单位进行查找。查找完成后打印到屏幕。
3.1 用法
grep -[options] 'pattern' <file_name>
或
<command> | grep -[option] 'pattern'
3.2 选项
-
-n
:line_number,显示行号 -
-v
:invert_match,反选 -
-i
:ignore,不区分大小写 -
-o
:only_matching,精确打印一行中匹配的部分 -
-c
:counts,仅显示命中的行数 -
-r
:recursive,递归地查找子目录中的文件 -
-e PATTERN
:如有多个RE,则每使用一个RE要加一次-e
,但是多个RE以“或”方式进行查找,若需要以“与”的方式进行多RE的查找,只能通过多层管道,一层层进行筛选 -
-A#
:after,同时显示命中行后的 # 行 -
-B#
:before,同时显示命中行前的 # 行 -
-C#
:before & after,同时显示命中行前&后的 # 行
3.3 选项示例
其实示例中应该包含RE相关的知识,但其实RE仅仅是个通用的表达式,跟工具无关。所以在本示例中不再加入过多关于RE使用技巧的介绍
- 环境介绍
[root@choco-01 grep]# pwd
/root/grep
[root@choco-01 grep]# ls
regular_express.txt
[root@choco-01 grep]# nl regular_express.txt
1 "Open Source" is a good mechanism to develop programs.
2 apple is my favorite food.
3 Football game is not use feet only.
4 this dress doesn't fit me.
5 However, this dress is about $ 3183 dollars.
6 GNU is free air not free beer.
7 Her hair is very beauty.
8 I can't finish the test.
9 Oh! The soup taste good.
10 motorcycle is cheap than car.
11 This window is clear.
12 the symbol '*' is represented as start.
13 Oh! My god!
14 The gd software is a library for drafting programs.
15 You are the best is mean you are the no. 1.
16 The world <Happy> is the same with "glad".
17 I like dog.
18 google is the best tools for search keyword.
19 goooooogle yes!
20 go! go! Let's go.
21 # I am VBird
- 无选项
[root@choco-01 grep]# grep 'GNU' regular_express.txt
GNU is free air not free beer.
-
-n
:打印行号
[root@choco-01 grep]# grep -n 'GNU' regular_express.txt
6:GNU is free air not free beer.
-
-v
:反选
[root@choco-01 grep]# grep -v 'o' regular_express.txt
Her hair is very beauty.
I can't finish the test.
\# I am VBird
-
-i
:不区分大小写
[root@choco-01 grep]# grep 'happy' regular_express.txt
[root@choco-01 grep]# grep -i 'happy' regular_express.txt
The world <Happy> is the same with "glad".
-
-o
:精确打印匹配字符
[root@choco-01 grep]# grep 'Happy' regular_express.txt
The world <Happy> is the same with "glad".
[root@choco-01 grep]# grep -o 'Happy' regular_express.txt
Happy
-
-c
:仅打印匹配的行数
[root@choco-01 grep]# grep 'god' regular_express.txt
Oh! My god!
[root@choco-01 grep]# grep -c 'god' regular_express.txt
1
-
-e
:多RE查找(或关系)
[root@choco-01 grep]# grep -n 'go' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
13:Oh! My god!
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
[root@choco-01 grep]# grep -n -e 'go' -e 'GNU' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
6:GNU is free air not free beer.
9:Oh! The soup taste good.
13:Oh! My god!
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
-
-A#
:after,同时打印匹配行的后 # 行
[root@choco-01 grep]# grep -n 'GNU' regular_express.txt
6:GNU is free air not free beer.
[root@choco-01 grep]# grep -n -A2 'GNU' regular_express.txt
6:GNU is free air not free beer.
7-Her hair is very beauty.
8-I can't finish the test.
-
-B#
:before,同时打印匹配行的前 # 行
[root@choco-01 grep]# grep -n 'GNU' regular_express.txt
6:GNU is free air not free beer.
[root@choco-01 grep]# grep -n -B2 'GNU' regular_express.txt
4-this dress doesn't fit me.
5-However, this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
-
-C#
:同时打印前&后的 # 行
[root@choco-01 grep]# grep -n 'GNU' regular_express.txt
6:GNU is free air not free beer.
[root@choco-01 grep]# grep -n -C2 'GNU' regular_express.txt
4-this dress doesn't fit me.
5-However, this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
7-Her hair is very beauty.
8-I can't finish the test.
4. sed
查找、截取、替换、新增、删除。
4.1 用法
sed [options] line1,line2[action] [file_name]
或
[command] | sed [options] line1,line2[action]
4.2 Option
-
-n
:静默模式,仅打印命中的行 -
-e
:edit,直接通过命令行模式进行 sed 动作的编辑 -
-f
:file,后接 sed 文件,表示直接以后接 sed 文件中的 sed 语句进行执行 -
-r
:RE-Extended,开启扩展RE的支持 -
-i
:使 sed 命令直接对文件内容进行更改,而不仅仅是对 stdout
4.3 Action
-
i
:向上增行,在命中的行的上一行新增,后接字符串 -
a
:向下增行,在命中的行的下一行新增,后接字符串 -
d
:行删除,删除命中行 -
p
:行打印,打印命中的数据 -
s
:字符替换,#,#s/old_pattern/new_pattern/g -
c
:行替换,替换命中的所有行为指定内容(多个行替换成一条新内容),后接字符串。 -
g
:全局替换,默认只替换每行第一个命中的字符,加上g
后则能替换整行的命中字符 -
\
:配合a、i使用,适用于增加的内容为多行的情况
4.3 示例
- 环境介绍
[root@choco-01 sed]# nl test.txt
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
-
p
与-n
:光是用p
进行打印,会在打印整个文件的基础上多打印一遍命中行,加上-n
后就仅打印命中行
[root@choco-01 sed]# nl test.txt | sed '3,4p'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@choco-01 sed]# nl test.txt | sed -n '3,4p'
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
-
d
:删除行
[root@choco-01 sed]# nl test.txt | sed '3,4d'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
-
a
:向下增行
[root@choco-01 sed]# nl test.txt | sed '2a this is a new line'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
this is a new line
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
-
i
:向上增行
[root@choco-01 sed]# nl test.txt | sed '2i this is a new line'
1 root:x:0:0:root:/root:/bin/bash
this is a new line
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
-
\
:增加内容为多行,多行内容以\+[Enter]
进行分隔,最后一行的末尾以后半个单引号结束
[root@choco-01 sed]# nl test.txt | sed '2i 1st added line\
> 2nd added line\
> 3rd added line'
1 root:x:0:0:root:/root:/bin/bash
1st added line
2nd added line
3rd added line
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
-
c
:替换行。上述的“增加内容为多行”的\
在此也适用
[root@choco-01 sed]# nl test.txt | sed '2,3c this line replaces the old 2&3 line'
1 root:x:0:0:root:/root:/bin/bash
this line replaces the old 2&3 line
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
-
s
:字符替换
sed 's/PATTERN/new/g'
[root@choco-01 sed]# nl test.txt
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@choco-01 sed]# nl test.txt | sed 's/sbin/SBIN/'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/SBIN/nologin
3 daemon:x:2:2:daemon:/SBIN:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/SBIN/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/SBIN/nologin
-
g
:全局替换
[root@choco-01 sed]# nl test.txt | grep 'root' | sed 's/root/ROOT/'
1 ROOT:x:0:0:root:/root:/bin/bash
[root@choco-01 sed]# nl test.txt | grep 'root' | sed 's/root/ROOT/g'
1 ROOT:x:0:0:ROOT:/ROOT:/bin/bash
-
i
:直接更改文件(默认情况下,sed只是修改文件打印到屏幕的样子,加上i
则直接改变文件)
[root@choco-01 sed]# nl test.txt
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@choco-01 sed]# sed -i 's/sbin/SBIN/' test.txt
[root@choco-01 sed]# nl test.txt
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/SBIN/nologin
3 daemon:x:2:2:daemon:/SBIN:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/SBIN/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/SBIN/nologin
5. awk
(The End)