#过滤出以I开头的行 [root@haoshuaicong ~]# grep '^I' /haoshuaicong/re.txt I am haoshuaicong teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess!
2.2 $ 以….结尾的行
#过滤出以数字8结尾的行 [root@haoshuaicong ~]# grep '8$' /haoshuaicong/re.txt my qq is 49000448 # 注: 假如最后一个字符是空格,使用 cat -A 可以显示
2.3 ^$ 空行,这行中没有任何字符
#过滤出文件中的空行并显示行号 [root@haoshuaicong ~]# grep -n '^$' /haoshuaicong/re.txt 2: 6: 10: #排除空行并显示行号,取反 [root@haoshuaicong ~]# grep -nv '^$' /haoshuaicong/re.txt 1:I am haoshuaicong teacher! 3:I teach linux. 4:I like badminton ball ,billiard ball and chinese chess! 5:my blog is http: ˌ haoshuaicong.blog.51cto.com 7:our size is http: ˌ blog.haoshuaicong.com 8:my qq is 49000448 9:not 4900000448. 11:my god ,i am not oldbey,but haoshuaicong! #排除井号和空行 grep -v '^$' /etc/profile | grep -v '^#'
2.4 . 任意一个字符
#过滤出oldb.y的行,点表示任意的一个字符 [root@haoshuaicong ~]# grep 'oldb.y' /haoshuaicong/re.txt I am haoshuaicong teacher! my blog is http: ˌ haoshuaicong.blog.51cto.com our size is http: ˌ blog.haoshuaicong.com my god ,i am not oldbey,but haoshuaicong! # 注: 过滤的时候会排除空行。点不会匹配空行。
2.5 \ 撬棍 转义字符 脱掉马甲打回原形,去掉特殊符号的含义
#过滤出带.的行 grep '\.' /haoshuaicong/re.txt
2.6 * 前一个字符连续出现0次或0次以上
贪婪性,只要连续就只匹配一次
2.7 .* 表示所有字符
#所有内容都grep出来 [root@haoshuaicong ~]# grep '.*' re.txt aaab aabaa acacacac #过滤出haoshuaicong [root@haoshuaicong ~]# grep 'ol.*oy' /haoshuaicong/re.txt I am haoshuaicong teacher! my blog is http: ˌ haoshuaicong.blog.51cto.com our size is http: ˌ blog.haoshuaicong.com my god ,i am not oldbey,but haoshuaicong!