|
六周第一次课(1月15日)
9.1 正则介绍_grep上
9.2 grep中
9.3 grep下
六周第二次课(1月16日)
9.4/9.5 sed
什么是正则
正则就是一串有规律的字符串
掌握好正则对于编写shell脚本有很大帮助
各种编程语言中都有正则,原理是一样的
本章将要学习grep/egrep、sed、awk
grep
- [ ] grep [-cinvABC] 'word' filename
- [ ] -c 行数
- [ ] -i 不区分大小写
- [ ] -n 显示行号
- [ ] -v 取反
- [ ] -r 遍历所有子目录
- [ ] -A 后面跟数字,过滤出符合要求的行以及下面n行
- [ ] -B 同上,过滤出符合要求的行以及上面n行
- [ ] -C 同上,同时过滤出符合要求的行以及上下各n行
举例:新建一个grep目录,将etc/passwd目录下的内容拷贝到grep目录下
[root@localhost ~]# mkdir grep
[root@localhost ~]# cd grep/
[root@localhost grep]# cp /etc/passwd .
[root@localhost grep]# ls
passwd
说明,gerp默认匹配到的字符串标注为红色,用which命令看一下,是带颜色自动显示的
[root@localhost grep]# grep -c 'nologin' passwd
38
- grep -n
- grep -v
- grep -r
如果不加r则显示:
[root@localhost grep]# grep 'root' /etc/
grep: /etc/: 是一个目录
- 通过grep遍历所有root文件并找到与password相关的
- grep -A2 会把包含root的行以及这行下面的两行打印出来
- grep -B2 会把包含root的行以及这行上面的两行打印出来
- grep -C2 会把包含root的行以及这行上下各两行打印出来
grep/egrep示例
- [ ] grep -n 'root' /etc/passwd
- [ ] grep -nv 'nologin' /etc/passwd
- [ ] grep '[0-9]' passwd
- [ ] grep -v '[0-9]'/etc/inittab
- grep -vn 举例,把不含数字的行打印出来,vim编辑时输入:set nu 提示行号
- [ ] grep -v '^#' /etc/inittab 为了便于测试,需要将/etc/inittab拷贝出来到gerp目录下
[root@localhost grep]# cp /etc/inittab ./ //拷贝的测试目录grep下
编辑inittab文件,把带#号的两行修改了,方便测试
这在后续查找文件时,方便阅读
- [ ] grep '[^0-9]' inittab
- [ ] grep '^[^0-9]' inittab
- [ ] grep 'r.o' passwd //‘r.o’中.表示任意一个字符
为方便测试需要vim其中的passwd文件,并添加r&o等特殊字符
- [ ] grep 'oo' passwd passwd,表示零个或多个*前面的字符,0~n个o
[root@localhost grep]# grep 'oo*' passwd|wc -l
46 //结果相同
[root@localhost grep]# grep 'o*o' passwd|wc -l
46
- [ ] grep '.' passwd passwd,.表示零个或多个任意字符,空行也包含在内,这样就把passwd里面所有的行都匹配到。
- [ ] grep 'o{2}' passwd passwd,这里{},其内部为数字,表示前面的字符要重复的次数,需要强调的是{}左右都要加上脱义字符\,另外{}还可以表示一个范围,{1,3}表示重复1到3次前面的字符,{1,}表示大于1次重复前面字符
- [ ] egrep 'o{2}' /etc/passwd 等同于上面的命令
== grep 'o\{2\}' passwd == grep -E 'o{2}' /etc/passwd
- [ ] egrep 'o+o'passwd,+表示匹配1个或者多个+前面的字符
[root@localhost grep]# grep 'o\+o' passwd |wc -l
6
[root@localhost grep]# egrep 'o+o' passwd |wc -l
6
- [ ] egrep 'o?t' passwd,过滤出零个或者一个指定的字符
- [ ] egrep 'root|nologin' passwd,|表示或者,可以有多个|
== grep -E
[root@localhost grep]# egrep 'root|nologin' passwd|wc -l
39
[root@localhost grep]# grep -E 'root|nologin' passwd|wc -l
39
- [ ] egrep '(oo){2}' passwd
sed工具使用
- sed -n,选项表示只显示我们要打印的行,请看-n和没有-n的区别
[root@localhost ~]# mkdir sed
[root@localhost ~]# cd sed
[root@localhost sed]# cp ../grep/passwd test.txt
[root@localhost sed]# sed -n '/root/'p test.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
- sed -r 这里-r和grep里面-E一样,还有加上了才能有效
支持+,*,.,|或者,与等
[root@localhost sed]# sed -n '/o+t/'p test.txt
[root@localhost sed]# sed -nr '/o+t/'p test.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
setroubleshoot:x:991:986::/var/lib/setroubleshoot:/sbin/nologin
- sed打印某行
#sed -n '2'p test.txt //打印第二行
sed -n '1,$'p test.txt //打印所有行
sed -n '1,5'p test.txt //打印1~5行
[root@localhost sed]# sed -n '1,5'p test.txt
root:x:0:0:root:/root:/bin/bash
adsda:deda:deded:road:ded
sasdda:deda:&&DE:r&o:r |
|
|