1. 正则表达式
正则表达式仅被三剑客命令(grep/egrep、sed、awk)支持,其他命令无法使用。
正则表达式以行为单位,一次处理一行。
1.1 BRE 基础正则表达式
# ^,匹配以指定字符开头的行
[root@oldboy test]# grep ^I oldboy.txt
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
# 过滤出目录
[root@oldboy test]# ls -l ~ | grep ^d
drwxr---w-. 2 root root 6 1月 13 10:46 d035
drwxr-xr-x. 2 root root 24 1月 15 14:28 test
# $,匹配以指定字符结尾的行;注意^和$的位置
[root@oldboy test]# grep com$ oldboy.txt
our site is http://www.oldboyedu.com
# 过滤出目录
[root@oldboy test]# ls -lF ~ | grep /$
drwxr---w-. 2 root root 6 1月 13 10:46 d035/
drwxr-xr-x. 2 root root 24 1月 15 14:28 test/
# ^$,表示空行
[root@oldboy test]# grep ^$ oldboy.txt -n
4:
8:
# .,匹配任意一个字符,不匹配空行
[root@oldboy test]# grep . oldboy.txt -n #去除空行
1:I am oldboy teacher!
2:I teach linux.
3:I like badminton ball ,billiard ball and chinese chess!
5:our site is http://www.oldboyedu.com
6:my qq num is 49000448
7:not 4900000448.
9:my god ,i am not oldbey,but OLDBoY!
# \,转义字符,去除特殊字符的特殊含义
[root@oldboy test]# grep "\.$" oldboy.txt -n #匹配以.结尾的行
2:I teach linux.
7:not 4900000448.
# *,匹配前一个字符出现0或n次
[root@oldboy test]# grep -n "0*" oldboy.txt
1:I am oldboy teacher!
2:I teach linux.
3:I like badminton ball ,billiard ball and chinese chess!
4:
5:our site is http://www.oldboyedu.com
6:my qq num is 49000448
7:not 4900000448.
8:
9:my god ,i am not oldbey,but OLDBoY!
[root@oldboy test]# grep -n "00*" oldboy.txt
6:my qq num is 49000448
7:not 4900000448.
# .*,匹配所有行,包括空行
[root@oldboy test]# grep -n ".*" oldboy.txt
1:I am oldboy teacher!
2:I teach linux.
3:I like badminton ball ,billiard ball and chinese chess!
4:
5:our site is http://www.oldboyedu.com
6:my qq num is 49000448
7:not 4900000448.
8:
9:my god ,i am not oldbey,but OLDBoY!
# 匹配集合内任意一个字符
[root@oldboy test]# grep -n [0-9] oldboy.txt # 匹配含有数字的行
6:my qq num is 49000448
7:not 4900000448.
# 取反,匹配不仅仅包含数字的行
[root@oldboy test]# grep -n [^0-9] oldboy.txt
1:I am oldboy teacher!
2:I teach linux.
3:I like badminton ball ,billiard ball and chinese chess!
5:our site is http://www.oldboyedu.com
6:my qq num is 49000448
7:not 4900000448.
9:my god ,i am not oldbey,but OLDBoY!
1.2 测试题
1.过滤/etc/passwd中以nologin结尾的行。
grep “nologin$” /etc/passwd
2.过滤/etc/passwd中以o开头的行。
grep “^o” /etc/passwd
3.过滤/etc/passwd中至少含有1个0字符串的行。
grep “00*” /etc/passwd
4.过滤/etc/passwd中的空行。
grep “^$” /etc/passwd
5.过滤/etc/目录中(不含子目录)下的所有文件。
ls -l /etc|grep “^-”
6.过滤/etc/services中含有点号的行。
grep “.” /etc/services
1.3 ERE 扩展正则表达式
使用扩展的正则表达式必须使用egrep
# +,匹配一次或多次
[root@oldboy test]# egrep -n "0+" oldboy.txt
6:my qq num is 49000448
7:not 4900000448.
# ?,匹配前一个字符0次或一次
[root@oldboy test]# egrep -n "oldboy?" oldboy.txt
1:I am oldboy teacher!
5:our site is http://www.oldboyedu.com
# |,或者
[root@oldboy test]# egrep -n "oldboy|com" oldboy.txt
1:I am oldboy teacher!
5:our site is http://www.oldboyedu.com
# (),空号中的内容作为一个整体;\n表示第n个空号的值
[root@oldboy test]# egrep -n "(0)\1" oldboy.txt # 匹配00
6:my qq num is 49000448
7:not 4900000448.
1.4 扩展正则表达式测试题
2. sed 字符流编辑器
sed,stream editor,字符流编辑器。
sed [选项] [sed内置命令字符] [输入文件]
# p:print,输出匹配行的内容;必须用单引号
[root@oldboy test]# sed -n '5,7p' oldboy.txt # 取文件的5到7行数据
our site is http://www.oldboyedu.com
my qq num is 49000448
not 4900000448.
[root@oldboy test]# sed -n '3p' oldboy.txt
I like badminton ball ,billiard ball and chinese chess!
# 过滤包含oldboy的行
[root@oldboy test]# sed -n '/oldboy/p' oldboy.txt
I am oldboy teacher!
our site is http://www.oldboyedu.com