码迷,mamicode.com
首页 > 其他好文 > 详细

正则介绍_grep

时间:2018-01-15 12:32:34      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:帮助   gre   大写   pass   root   上下   ks.cfg   code   就是   

9.1 正则介绍_grep(上)

什么是正则
正则是一串有规律的字符串
掌握好正则对于编写shell脚本有很大的帮助
各种编程语言中都有正则,原理是一样的
本章将要学习grep/egrep、sed、awk
grep
grep[-cinvABC] ‘word‘ filename
-c 行数
-i 不区分大小写
-n 显示行号
-v 取反
-r遍历所有子目录
-A 后面跟数字,过滤出符合要求的行以及下面n行
-B同上,过滤出符合要求的行以及上面n行
-c同上,同时过滤出符合要求的行以及上下各n行

[root@centos-01 ~]# ls
11.txt  123  1.txt  1.txt~  234  2.txt  3.txt  anaconda-ks.cfg.1  default
[root@centos-01 ~]# mkdir grep
[root@centos-01 ~]# cd grep
[root@centos-01 grep]# cp /etc/passwd .
[root@centos-01 grep]# ls
passwd
[root@centos-01 grep]# pwd
/root/grep
[root@centos-01 grep]# ls
passwd
[root@centos-01 grep]# grep ‘nologin‘ passwd
[root@centos-01 grep]# grep -c ‘nologin‘ passwd          //加-c看有多少行//
18
[root@centos-01 grep]# grep -n ‘nologin‘ passwd          //加-n显示行号//
[root@centos-01 grep]# grep -ni ‘nologin‘ passwd         //加i会把大写的一列列出来//

9.2 grep(中)

grep ‘[0-9]‘ /etc/inittab

[root@centos-01 grep]# grep ‘[0-9]‘ passwd

grep -v‘[0-9]‘ /etc/inittab //不带数字的行//
grep -v‘^#‘/etc/inittab //^表示以什么开头的行,这里表示以#开头的行,v表示不是以什么开头的行//
grep ‘^[^a-zA-Z]‘test.txt
^如果在[]中则取非的意思,如果在[ ]外面,则是以什么什么开头的意思

9.3 grep (下)

grep ‘r.o‘test.txt //小数点表示任意一个字符//
grep ‘oo‘test.txt //表示0个或者多个号前面的字符//
grep ‘.
‘test.txt //点*就是通配//
grep ‘o{2}‘/etc/passwd //表示一个范围//
egrep ‘o+‘/etc/passwd //表示一个或多个加号前面的字符//
egrep ‘oo?‘/etc/passwd //表示0个或一个问号前面的字符//
egrep ‘root|nologin‘ /etc/passwd //竖线表示或者//

正则介绍_grep

标签:帮助   gre   大写   pass   root   上下   ks.cfg   code   就是   

原文地址:http://blog.51cto.com/13242922/2061010

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!