用于过滤文本时的附加条件,用来更精确的过滤文本内容。
[:alnum] 代表所以的大小写英文字符和数字 0-9 A—Z a-z
[:alpha:] 代表任意英文大小写字符 A-Z a-z
[:lower:] 代表小写字符 a-z
[:upper:] 代表大写字符 A-Z
[:digit:] 代表数字 0-9
wget http://linux.vbird.org/linux_basic/0330regularex/regular_express.txt 获取文本
开始试验
grep -n "the" regular_express.txt 打印包含the的行,并打印行号
grep -in "the" regular_express.txt 打印包含the的行,并打印行号,并忽略大小写
grep -inv "the" regular_express.txt 打印不包含the的行,并打印行号,并忽略大小写
正则
-bash-4.2# grep -n "t[ea]st" regular_express.txt #[]括号里的‘e,a’有且只有一个
8:I can't finish the test.
9:Oh! The soup taste good.
-bash-4.2# grep -n "[^g]oo" regular_express.txt #查询不已g为开头且包含oo的行
2:apple is my favorite food.
3:Football game is not use feet only.
19:google is the best tools for search keyword.
20:goooooogle yes!
21:goooooooood!
23:gooogle yesssssssss!
相对应的
-bash-4.2# grep -n "^[g]oo" regular_express.txt #查询以goo为开头行
19:google is the best tools for search keyword.
20:goooooogle yes!
21:goooooooood!
22:google 12!
23:gooogle yesssssssss!
[root@rourou ~]# grep -n '[0-9]' regular_express.txt
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1. 找包含0-9的行
并且
[root@rourou ~]# grep -n '[0-9][0-9]*' regular_express.txt
5:However, this dress is about $ 3183 dollars. 找包含0-9的行,后一个0-9后面加了*所以后一
15:You are the best is mean you are the no. 1. 个0-9可以重复0到n次
同样的
[root@rourou ~]# grep -n '[0-9][0-9][0-9]*' regular_express.txt
5:However, this dress is about $ 3183 dollars. 与上一题一样,不过必须是两位数及两位数以上的
[root@rourou ~]# grep -n '^$' regular_express.txt
25: 以换行符为开头,所以查到的为空白行
[root@rourou ~]# grep -n 'g..d' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good. 查询g与d中间间隔为2的行
16:The world <Happy> is the same with "glad".
同样的
[root@rourou ~]# grep -n 'g...d' regular_express.txt
26:goood
27:gwewd 查询g与d中间间隔为3的行
grep -n 'go\{2,5\}g' regular_express.txt 筛选g与g之间有2到5个o,\的作用在于将特殊字符的特殊含
义去掉.o为判定字符,因此第一个o不计算
grep扩展
-An: 查找行的后几行
-Bn: 查找行的前几行
[root@rourou ~]# grep mail /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
所以:
[root@rourou ~]# grep mail -A2 -B3 /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
[root@rourou ~]# grep -o mail /etc/passwd -o 只输出查找项
mail
mail
mail
[root@rourou ~]# grep -o -c 'mail' /etc/passwd -c 统计数量
1
[root@rourou ~]# grep -l mail /etc/passwd
/etc/passwd -l 得到文件路径
grep -r root /etc -r 在/etc下查找包含root的行
扩展正则
[root@rourou ~]# egrep -n 'go+d' regular_express.txt 包含1到n个o的行
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
13:Oh! My god!
[root@rourou ~]# egrep -n 'go?d' regular_express.txt 包含0个或1个o
13:Oh! My god!
14:The gd software is a library for drafting programs.
[root@rourou ~]# egrep -n 'go|goo' regular_express.txt go与goo是或的关系
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@rourou ~]# egrep -n 'g(o|oo)d' regular_express.txt g与d是固定的,o与oo是或的关系
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
13:Oh! My god!
[root@rourou ~]# echo 'AxyzxyzxyzC' | egrep 'A(xyz)+C' 重复部分用单个的代替
AxyzxyzxyzC
前面的即使不是连续的也是可以的
[root@rourou ~]# echo 'zxyzxyzzxyyxzy' | egrep [xyz]+
zxyzxyzzxyyxzy
网友评论